How to create a image picker on Xamarin Forms?

和自甴很熟 提交于 2020-07-07 12:00:26

问题


Does anybody knows if its possible to make a Image picker like this:

I've tried with the followings plugins :

https://github.com/jamesmontemagno/MediaPlugin

https://github.com/matheusneder/Xamarin.Forms.ImagePicker

I do not want to create buttons to be able to perform each operation, what I want is that with a single button I suggest which application to use. This is possible?

Thanks.


回答1:


If you want to pick a photo from the phone's picture library, due to Xamarin.Forms does not include this functionality, it is necessary to use DependencyService to access native APIs on each platform.

Create the interface: IPhotoPickerService.cs

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

Android implementation:

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

For more information about IOS, UWP implementation of photo picker, you could check the MS article. https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/dependency-service/photo-picker

And download the source file from link. https://docs.microsoft.com/zh-cn/samples/xamarin/xamarin-forms-samples/dependencyservice/




回答2:


You can use Intent.SetComponent on Android via xf dependency service:

Xf:

public interface IShare
{
    void Share();
}

Android:

[assembly: Dependency(typeof(ShareImage))]
namespace AndroidShareSample.Droid
{
    public class ShareImage : Activity, IShare
    {
        public void Share()
        {
            var appPackageName = "com.sample.app";
            var intent = new Intent(Intent.ActionPick, Android.Provider.MediaStore.Images.Media.ExternalContentUri);
            var resInfos = CrossCurrentActivity.Current.Activity.PackageManager.QueryIntentActivities(intent,0);
            intent.SetComponent(new ComponentName(appPackageName, resInfos.FirstOrDefault(x=> x.ActivityInfo.PackageName == appPackageName).ActivityInfo.Name));
            Forms.Context.StartActivity(Intent.CreateChooser(intent, "Select Image"));
        }
    }
}


来源:https://stackoverflow.com/questions/59002371/how-to-create-a-image-picker-on-xamarin-forms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!