Xamarin 从TakePhotoAsync()获取图像流
我正在从TakePhotoAsync方法获取图像流?我需要在哪里更改代码Xamarin 从TakePhotoAsync()获取图像流,xamarin,Xamarin,我正在从TakePhotoAsync方法获取图像流?我需要在哪里更改代码 await CrossMedia.Current.Initialize(); if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported) { await DisplayAlert("Alert", "No camera is avai
await CrossMedia.Current.Initialize();
if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
{
await DisplayAlert("Alert", "No camera is available!", "OK");
return;
}
camreaFile = await CrossMedia.Current.TakePhotoAsync(new StoreCameraMediaOptions
{
PhotoSize = Plugin.Media.Abstractions.PhotoSize.Small,
//SaveToAlbum = true,
Directory = "Sample",
Name = "test.jpg"
});
//mediaFile = await CrossMedia.Current.TakePhotoAsync();
if (camreaFile == null)
return;
fileName.Text = camreaFile.AlbumPath;
await DisplayAlert("File Location", camreaFile.Path, "OK");
image.Source = ImageSource.FromStream(() =>
{
var stream = camreaFile.GetStream();
//camreaFile.Dispose();
return stream;
});
-服务器的Pass参数
if(mediaFile !=null)
{
MultipartFormDataContent content = new
MultipartFormDataContent();
content.Add(new StringContent(AppSetting.UserName),
"userId");
content.Add(new StringContent(remark), "remarks");
content.Add(new StringContent(AppSetting.ConsignmentNumebr), "consignmentNo");
content.Add(new StringContent(AppSetting.TaskId), "jobId");
content.Add(new StreamContent(cameraFile.GetStream()),"\"file\"",$"\"{AppSetting.ConsignmentNumebr + ".png"}\"");
var httpClient = new HttpClient();
var uploadServiceBaseAddress = "myserver/api/UploadContainerImage/"; var uploadResponseMessage = await httpClient.PostAsync(uploadServiceBaseAddress, content);uploadResponseMessage.Content.ReadAsStringAsync(); }
-我希望输出能够成功地到达服务器。在将媒体文件发送到服务器后处理它,以避免此异常 当然,如果在将其发送到服务器之前执行此操作,它将为您提供一个 如果要处理此变量,请在服务调用后执行以下操作:
FooServiceMethod();
mediaFile.Dispose();
您的问题不明确,您到底想在这里实现什么?当我使用以下代码->content.Addnew StreamContentcameraFile.GetStream、\file\,$\{AppSetting.CommissionNumebr+.png}将文件上载到服务器时;我无法访问Dispose对象时出错。这是因为一旦您离开fromStream方法,您的流将被释放,您将不得不在另一个变量中维护它。是的,。我忘记删除mediaFile.Dispose。当我删除它时,我工作正常。谢谢,@G.hakim为我指点。如果您需要,我可以添加它作为答案!