【问题标题】:xamarin android image picker - unable to get pathxamarin android图像选择器 - 无法获取路径
【发布时间】:2021-02-19 18:08:11
【问题描述】:

当我打开图库并转到保管箱或一个驱动器时,我收到一条消息无法获取路径,但是当我转到 google 照片或本地时,我得到了正确的图像以加载到网格中。

 DependencyService.Get<IMediaService>().OpenGallery();

 MessagingCenter.Unsubscribe<App, List<string>>((App)Xamarin.Forms.Application.Current, "ImagesSelectedAndroid");
           
 MessagingCenter.Subscribe<App, List<string>>((App)Xamarin.Forms.Application.Current, "ImagesSelectedAndroid", (s, images) =>
                    {
                

                        if (images.Count > 0)
                        {
                            Console.WriteLine($"Processed {images.Count} images");
                            UploadToBlob(images);
                            GetGallery();
                        }

 public void OpenGallery()
        {
            try
            {
                var imageIntent = new Intent(Intent.ActionPick);
                imageIntent.SetType("image/*");
                imageIntent.PutExtra(Intent.ExtraAllowMultiple, true);
                imageIntent.SetAction(Intent.ActionGetContent);
                ((Activity)CrossCurrentActivity.Current.Activity).StartActivityForResult(Intent.CreateChooser(imageIntent, "Select photo"), Opengallerycode);
                Toast.MakeText(CrossCurrentActivity.Current.Activity, "Tap and hold to select multiple photos.", ToastLength.Short)?.Show();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                Toast.MakeText(CrossCurrentActivity.Current.Activity, "Error. Can not continue, try again.", ToastLength.Long)?.Show();
            }
        }

【问题讨论】:

  • 如何使用GetGallery方法获取图库?
  • 我刚刚添加了我创建的 opengallery 类来打开它。
  • 我建议使用流而不是路径来获取图像。

标签: android xamarin dropbox imagepicker


【解决方案1】:

您可以将图像从 Dropbox 或一个驱动器导入流并加载到网格中。

创建接口: IPhotoPickerService.cs

public interface IPhotoPickerService
{
    Task<Stream> GetImageStreamAsync();
}

Android 实现:

MainActivity.cs

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    internal static MainActivity Instance { get; private set; }

      ... ...

    // Field, property, and method for Picture Picker
    public static readonly int PickImageId = 1000;

    public TaskCompletionSource<Stream> PickImageTaskCompletionSource { set; get; }

    protected override void OnActivityResult(int requestCode, Result resultCode, Intent intent)
    {
        base.OnActivityResult(requestCode, resultCode, intent);

        if (requestCode == PickImageId)
        {
            if ((resultCode == Result.Ok) && (intent != null))
            {
                Android.Net.Uri uri = intent.Data;
                Stream stream = ContentResolver.OpenInputStream(uri);

                // Set the Stream as the completion of the Task
                PickImageTaskCompletionSource.SetResult(stream);
            }
            else
            {
                PickImageTaskCompletionSource.SetResult(null);
            }
        }
    }
}

PhotoPickerService.cs

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

更多关于IOS、UWP实现照片选择器的信息,可以查看MS文章。 https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/dependency-service/photo-picker

并从链接下载源文件。 https://docs.microsoft.com/zh-cn/samples/xamarin/xamarin-forms-samples/dependencyservice/

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-16
相关资源
最近更新 更多