从TakePhotoAsync()获取图像流

时间:2019-02-18 04:10:11

标签: xamarin

我正在从TakePhotoAsync方法获取图像流吗?我需要在哪里更改代码?

           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;
        });

-服务器的传递参数

 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();          }

-我希望输出成功到达服务器。

1 个答案:

答案 0 :(得分:1)

将媒体文件发送到服务器后,请处理该文件,以免发生此异常。

如果您在将其发送到服务器之前执行了此操作,那么它将为您提供ObjectDisposedException

如果要处置此变量,只需在服务调用后执行以下操作:

FooServiceMethod();
mediaFile.Dispose();