Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select multiple images from gallery for android and iOS device using xamarin.forms?

I am working on xamarin.forms. I am creating an app that can run on Android and iOS both. I have to select multiple images from gallery for both devices.

But, I am unable to select multiple images at a time. I can select single image with help of media picker (CrossMedia).

Please update me how I can select multiple images from gallery for both devices using xamarin.forms?

Regards, Anand Dubey

like image 384
anand Avatar asked Nov 29 '25 05:11

anand


1 Answers

Firstly for select multiple images in Xamarin Forms, you should do a dependency service for each platform.

In Android use:

.PutExtra (Intent.ExtraAllowMultiple, true);

Ex:

  [assembly: Xamarin.Forms.Dependency (typeof (MediaService))]
  namespace MyProject
  {
  public class MediaService : Java.Lang.Object, IMediaService
  {
    public MediaService ()
    {
    }

    public void OpenGallery()
    {

        Toast.MakeText (Xamarin.Forms.Forms.Context, "Select max 20 images", ToastLength.Long).Show ();
        var imageIntent = new Intent(
            Intent.ActionPick);
        imageIntent.SetType ("image/*");
        imageIntent.PutExtra (Intent.ExtraAllowMultiple, true);
        imageIntent.SetAction (Intent.ActionGetContent);
        ((Activity)Forms.Context).StartActivityForResult(
            Intent.CreateChooser (imageIntent, "Select photo"), 0);



       }

     }
   }

In IOS:

Install this control: ELCImagePicker

And:

[assembly: Xamarin.Forms.Dependency (typeof (MediaService))]
namespace MyProject
{
public class MediaService: IMediaService, IMessanger
{

    public MediaService ()
    {
    }
    public void OpenGallery()
    {

        var picker = ELCImagePickerViewController.Instance;
        picker.MaximumImagesCount = 15;

        picker.Completion.ContinueWith (t => {
            picker.BeginInvokeOnMainThread(()=>
                {
                    //dismiss the picker
                    picker.DismissViewController(true,null);

                    if (t.IsCanceled || t.Exception != null) {
                    } else {
                        Util.File.Path = new List<string>();

                        var items = t.Result as List<AssetResult>;
                        foreach (var item in items) {
                            Util.File.Path.Add (item.Path);
                        }

                        MessagingCenter.Send<IMessanger> (this, "PostProject");
                    }
                });
        });
        var topController = UIApplication.SharedApplication.KeyWindow.RootViewController;
        while (topController.PresentedViewController != null) {
            topController = topController.PresentedViewController;
        }
        topController.PresentViewController (picker, true, null);
    }
  }
}
like image 198
charlin agramonte Avatar answered Dec 01 '25 18:12

charlin agramonte



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!