媒体插件挑选图片不起作用

时间:2018-07-07 13:02:22

标签: c# xaml xamarin xamarin.forms

我是Xamarin的新手。我想使用媒体插件Xamarin.Forms来选择一张照片

我遵循了以下链接:(click here),我只遵循了拍摄图片的步骤,但是它不起作用并且出现错误

这是c#代码:

 public MyImage()
    {
        InitializeComponent();
       // string PhotoName = "kakashi.jpg";
       // MyProfilePhoto(PhotoName);



        pickPhoto.Clicked += async (sender, args) =>
        {
            if (!Plugin.Media.CrossMedia.Current.IsPickPhotoSupported)
            {
                await DisplayAlert("Photos Not Supported", ":( Permission not granted to photos.", "OK");
                return;
            }
            try
            {
                Stream stream = null;
                var file = await CrossMedia.Current.PickPhotoAsync().ConfigureAwait(true);


                if (file == null)
                    return;

                stream = file.GetStream();
                file.Dispose();

                image.Source = ImageSource.FromStream(() => stream);

            }
            catch (Exception ex)
            {
                // Xamarin.Insights.Report(ex);
                 await DisplayAlert("Uh oh", ex.ToString(), "OK");
            }
        };
    }

这是Xaml代码:

<StackLayout Spacing="10" Padding="10">

        <Button x:Name="pickPhoto" Text="Pick Photo"/>

        <Label Text="Image will show here"/>
        <Image x:Name="image"/>
        <Label Text=""/>

    </StackLayout>

这是错误:

image of error

如果您有更好的解决方案,我会采用

1 个答案:

答案 0 :(得分:0)

尝试以下代码从图库中读取照片,

public static async Task<String> getGallery()
    {
        var storageStatus = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Storage);

        if (storageStatus != PermissionStatus.Granted)
        {
            try
            {
                var results = await CrossPermissions.Current.RequestPermissionsAsync(new[] { Permission.Camera, Permission.Storage });
                storageStatus = results[Permission.Storage];
            }
            catch (Exception ex)
            {
                Debug.WriteLine(@"ERROR {0}", ex.Message);
                return null;
            }
        }

        if (storageStatus == PermissionStatus.Granted)
        {
            //Solution for straight base64 conversion
            try
            {
                var media = CrossMedia.Current;
                //var file;
                MediaFile file = null;
                if (CrossMedia.Current.IsPickPhotoSupported)
                {
                    await media.PickPhotoAsync(new PickMediaOptions
                    {
                        PhotoSize = PhotoSize.Medium,
                        CompressionQuality = 50
                    }).ContinueWith(t =>
                    {
                        if (!t.IsCanceled)
                            file = t.Result;
                    }, TaskScheduler.FromCurrentSynchronizationContext());
                }
                else
                {
                    MessagingService.Current.SendMessage<MessagingServiceAlert>(MessageKeys.DisplayAlert, new MessagingServiceAlert()
                    {
                        Title = MessageConsts.WarnTitle,
                        Message = MessageConsts.GallCamPermNotAvail,
                        Cancel = MessageConsts.BtnOk
                    });
                }

                String base64 = null;
                if (file != null)
                    base64 = await toBase64(file);

                return base64;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.StackTrace);
            }
            return null;
        }
        else
        {

            MessagingService.Current.SendMessage<MessagingServiceAlert>(MessageKeys.DisplayAlert, new MessagingServiceAlert()
            {
                Title = MessageConsts.WarnTitle,
                Message = MessageConsts.GallCamPermNotAvail,
                Cancel = MessageConsts.BtnOk
            });

            //On iOS you may want to send your user to the settings screen.
            if (Device.RuntimePlatform == Device.iOS)
                CrossPermissions.Current.OpenAppSettings();
        }

        return null;
    }

希望这会有所帮助。