SHARE
TWEET

Untitled

a guest Oct 23rd, 2019 96 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public static int OPENGALLERYCODE = 100;
  2. protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
  3. {
  4.     base.OnActivityResult(requestCode, resultCode, data);
  5.  
  6.     //If we are calling multiple image selection, enter into here and return photos and their filepaths.
  7.     if (requestCode == OPENGALLERYCODE && resultCode == Result.Ok)
  8.     {
  9.         List<string> images = new List<string>();
  10.  
  11.         if (data != null)
  12.         {
  13.             //Separate all photos and get the path from them all individually.
  14.             ClipData clipData = data.ClipData;
  15.             if (clipData != null)
  16.             {
  17.                 for (int i = 0; i < clipData.ItemCount; i++)
  18.                 {
  19.                     ClipData.Item item = clipData.GetItemAt(i);
  20.                     Android.Net.Uri uri = item.Uri;
  21.                     var path = GetRealPathFromURI(uri);
  22.  
  23.  
  24.                     if (path != null)
  25.                     {
  26.                         images.Add(path);
  27.                     }
  28.                 }
  29.             }
  30.             else
  31.             {
  32.                 Android.Net.Uri uri = data.Data;
  33.                 var path = GetRealPathFromURI(uri);
  34.  
  35.                 if (path != null)
  36.                 {
  37.                     images.Add(path);
  38.                 }
  39.             }
  40.  
  41.             //Send our images to the carousel view.
  42.             MessagingCenter.Send<App, List<string>>((App)Xamarin.Forms.Application.Current, "ImagesSelectedAndroid", images);
  43.         }
  44.     }
  45. }
  46.  
  47. /// <summary>
  48. ///     Get the real path for the current image passed.
  49. /// </summary>
  50. public String GetRealPathFromURI(Android.Net.Uri contentURI)
  51. {
  52.     try
  53.     {
  54.         ICursor imageCursor = null;
  55.         string fullPathToImage = "";
  56.  
  57.         imageCursor = ContentResolver.Query(contentURI, null, null, null, null);
  58.         imageCursor.MoveToFirst();
  59.         int idx = imageCursor.GetColumnIndex(MediaStore.Images.ImageColumns.Data);
  60.  
  61.         if (idx != -1)
  62.         {
  63.             fullPathToImage = imageCursor.GetString(idx);
  64.         }
  65.         else
  66.         {
  67.             ICursor cursor = null;
  68.             var docID = DocumentsContract.GetDocumentId(contentURI);
  69.             var id = docID.Split(':')[1];
  70.             var whereSelect = MediaStore.Images.ImageColumns.Id + "=?";
  71.             var projections = new string[] { MediaStore.Images.ImageColumns.Data };
  72.  
  73.             cursor = ContentResolver.Query(MediaStore.Images.Media.InternalContentUri, projections, whereSelect, new string[] { id }, null);
  74.             if (cursor.Count == 0)
  75.             {
  76.                 cursor = ContentResolver.Query(MediaStore.Images.Media.ExternalContentUri, projections, whereSelect, new string[] { id }, null);
  77.             }
  78.             var colData = cursor.GetColumnIndexOrThrow(MediaStore.Images.ImageColumns.Data);
  79.             cursor.MoveToFirst();
  80.             fullPathToImage = cursor.GetString(colData);
  81.         }
  82.         return fullPathToImage;
  83.     }
  84.     catch (Exception ex)
  85.     {
  86.         Toast.MakeText(Xamarin.Forms.Forms.Context, "Unable to get path", ToastLength.Long).Show();
  87.     }
  88.     return null;
  89. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top