Forum Xamarin.Forms

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

How to select multiple images from gallery from both android and iOS device?

2»

Answers

  • BadamSujithKumarBadamSujithKumar USMember ✭✭
    edited August 2018

    @Charlin I have implement the Multiple images selection in Xamarin forms , But it is not working in samsung devices . Please help anyone in this issue..

  • RedoRedo IDMember ✭✭

    @NagAddala How about Android? I need to implement it in Android and IOS

  • Ganesh.4229Ganesh.4229 USMember ✭✭

    @Redo @NagAddala @BadamSujithKumar
    Did you guys get any solution for Android. I have done it for iOS but looking for Android implementation.
    Could you please suggest if any solutions.

    Thank you,

  • amirvenusamirvenus USMember ✭✭✭

    @Ganesh.4229 said:
    @Redo @NagAddala @BadamSujithKumar
    Did you guys get any solution for Android. I have done it for iOS but looking for Android implementation.
    Could you please suggest if any solutions.

    Thank you,

    Could you please share your code with us? I am struggling to do it on iOS in a Xamarin.Forms project using DependencyService.

    Also, ECLImagePicker seems to be vanished!

  • Manisha97Manisha97 Member ✭✭

    Thanks for posting that Charlin :-)

  • Manisha97Manisha97 Member ✭✭

    ` protected override void OnActivityResult(int requestCode, Result resultCode, Intent intent)
    {
    base.OnActivityResult(requestCode, resultCode, intent);
    bool braked = false;
    string path = "";

            try
            {
                if (resultCode == Result.Ok)
                {
                    List<string> images = new List<string>();
                    if (intent != null)
                    {
                        ClipData clipData = intent.ClipData;
                        if (clipData != null)
                        {
    
                            for (int i = 0; i < clipData.ItemCount; i++)
                            {
    
                                if (i > 19)
                                {
                                    braked = true;
                                    break;
                                }
                                ClipData.Item item = clipData.GetItemAt(i);
                                global::Android.Net.Uri uri = item.Uri;
    
                                //In case you need image's absolute path
                                path = GetPathToImage(uri);
                                images.Add(path);
                            }
    
                        }
                        else
                        {
                            global::Android.Net.Uri uri = intent.Data;
                            path = GetPathToImage(uri);
                            images.Add(path);
                        }
    
                        MessagingCenter.Send<App, List<string>>((App)Xamarin.Forms.Application.Current, "ImagesForView", images);
    
                        if (braked == true)
                        {
                            // Toast.MakeText (Xamarin.Forms.Forms.Context, "Only the top 20 images will be uploaded", ToastLength.Long).Show ();
                        }
    
    
    
                    }
                }
                //Send the images to forms
            }
            catch (Exception ex)
            {
    
                //Toast.MakeText (Xamarin.Forms.Forms.Context, "Unable to open, error:" + ex.ToString(), ToastLength.Long).Show ();
            }
        }
    
        private string GetPathToImage(global::Android.Net.Uri uri)
        {
            string doc_id = "";
            using (var c1 = ContentResolver.Query(uri, null, null, null, null))
            {
                c1.MoveToFirst();
                String document_id = c1.GetString(0);
                doc_id = document_id.Substring(document_id.LastIndexOf(":") + 1);
            }
    
            string path = null;
    
            // The projection contains the columns we want to return in our query.
            string selection = MediaStore.Images.Media.InterfaceConsts.Id + " =? ";
            using (var cursor = ManagedQuery(MediaStore.Images.Media.ExternalContentUri, null, selection, new string[] { doc_id }, null))
            {
                if (cursor == null) return path;
                var columnIndex = cursor.GetColumnIndexOrThrow(MediaStore.Images.Media.InterfaceConsts.Data);
                cursor.MoveToFirst();
                path = cursor.GetString(columnIndex);
            }
            return path;
        }`
    
Sign In or Register to comment.