【问题标题】:Xamarin Essentials Media Picker Keeps Crashing My App - No Exception or errosXamarin Essentials 媒体选择器不断崩溃我的应用程序 - 没有异常或错误
【发布时间】:2021-01-05 20:41:02
【问题描述】:

我构建了一个 Xamarin 应用程序:Android,并试图让用户能够设置头像。使用 Xamarin Essentials Media Picker 我正在尝试捕获图像或选择一个图像。但是每次应用程序运行任一方法时,它都会运行,然后在选择或捕获图像之前使应用程序崩溃。有趣的是它有时有效,但几乎没有。

我尝试了很多方法来弄清楚发生了什么,但没有实际的错误可以解决,我一无所获。

我正在使用 MVVM 设计模式。 我的代码:

    private async Task TakePicture()
    {
        try
        {
            var photo = await MediaPicker.PickPhotoAsync(); 
            if (photo != null)
            {
                await App.UserManager.UpdateAvatarAsync(photo);
                RenderImages();
            }
        }
        catch (global::System.Exception ex)
        {
            await AppShell.Current.DisplayAlert("Oops", "Something went wrong and its not your fault", "Okay");
        }

    }

【问题讨论】:

  • 设备日志显示什么?您是否尝试过像 raygun.io 或 appcenter.ms 这样的崩溃报告服务?您是否正确检查了权限?
  • @Jason - 我认为我已经正确涵盖的权限。不会说谎我对 Xamarin 还很陌生,所以我没有尝试过崩溃报告服务。我想我现在首先按照您的建议检查设备日志。
  • @Jason 设备日志没有显示任何内容。
  • 代码看起来没问题。您是否尝试在其他设备上进行测试?它会崩溃吗?
  • @SechabaMotaung 我提供了两种选择照片的方法。你可以检查一下。

标签: android xamarin xamarin.forms xamarin.android xamarin.essentials


【解决方案1】:

您可以使用以下方式来挑选照片。

1.使用Xam.Plugin.Media您可以从 NuGet 安装。不要忘记检查您保存照片的位置并请求运行时权限。

下面的代码展示了如何从相机中选择照片并设置到图像控件。

  pickPhoto.Clicked += async (sender, args) =>
  {
    if (!CrossMedia.Current.IsPickPhotoSupported)
    {
      DisplayAlert("Photos Not Supported", ":( Permission not granted to photos.", "OK");
      return;
    }
     var file = await Plugin.Media.CrossMedia.Current.PickPhotoAsync(new Plugin.Media.Abstractions.PickMediaOptions
                  {
                      PhotoSize =  Plugin.Media.Abstractions.PhotoSize.Medium,
                
                  });


    if (file == null)
      return;

    image.Source = ImageSource.FromStream(() =>
    {
      var stream = file.GetStream();
      file.Dispose();
      return stream;
    });
  };

2。使用依赖服务。

[assembly: Dependency(typeof(PhotoPickerService))]
namespace DependencyServiceDemos.Droid
{
public class PhotoPickerService : IPhotoPickerService
{
    public Task<Stream> GetImageStreamAsync()
    {
        // Define the Intent for getting images
        Intent intent = new Intent();
        intent.SetType("image/*");
        intent.SetAction(Intent.ActionGetContent);

        // Start the picture-picker activity (resumes in MainActivity.cs)
        MainActivity.Instance.StartActivityForResult(
            Intent.CreateChooser(intent, "Select Photo"),
            MainActivity.PickImageId);

        // Save the TaskCompletionSource object as a MainActivity property
        MainActivity.Instance.PickImageTaskCompletionSource = new TaskCompletionSource<Stream>();

        // Return Task object
        return MainActivity.Instance.PickImageTaskCompletionSource.Task;
    }
}
}

您可以从下面的链接下载源文件。 https://docs.microsoft.com/en-us/samples/xamarin/xamarin-forms-samples/dependencyservice/

【讨论】:

  • 感谢您的回答。我会尽快尝试并实施它并给你反馈。目前,我必须快速推进,因为这个项目对时间很敏感。
  • 很高兴为您提供帮助。如果您对此有任何问题,请随时告诉我。如果此回复对您有帮助,请不要忘记接受答案。
猜你喜欢
  • 2017-10-23
  • 2019-01-22
  • 2020-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-16
  • 1970-01-01
相关资源
最近更新 更多