anand
anand

Reputation: 1459

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

Upvotes: 3

Views: 10980

Answers (1)

charlin agramonte
charlin agramonte

Reputation: 707

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

Upvotes: 12

Related Questions