Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static int OPENGALLERYCODE = 100;
- protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
- {
- base.OnActivityResult(requestCode, resultCode, data);
- //If we are calling multiple image selection, enter into here and return photos and their filepaths.
- if (requestCode == OPENGALLERYCODE && resultCode == Result.Ok)
- {
- List<string> images = new List<string>();
- if (data != null)
- {
- //Separate all photos and get the path from them all individually.
- ClipData clipData = data.ClipData;
- if (clipData != null)
- {
- for (int i = 0; i < clipData.ItemCount; i++)
- {
- ClipData.Item item = clipData.GetItemAt(i);
- Android.Net.Uri uri = item.Uri;
- var path = GetRealPathFromURI(uri);
- if (path != null)
- {
- images.Add(path);
- }
- }
- }
- else
- {
- Android.Net.Uri uri = data.Data;
- var path = GetRealPathFromURI(uri);
- if (path != null)
- {
- images.Add(path);
- }
- }
- //Send our images to the carousel view.
- MessagingCenter.Send<App, List<string>>((App)Xamarin.Forms.Application.Current, "ImagesSelectedAndroid", images);
- }
- }
- }
- /// <summary>
- /// Get the real path for the current image passed.
- /// </summary>
- public String GetRealPathFromURI(Android.Net.Uri contentURI)
- {
- try
- {
- ICursor imageCursor = null;
- string fullPathToImage = "";
- imageCursor = ContentResolver.Query(contentURI, null, null, null, null);
- imageCursor.MoveToFirst();
- int idx = imageCursor.GetColumnIndex(MediaStore.Images.ImageColumns.Data);
- if (idx != -1)
- {
- fullPathToImage = imageCursor.GetString(idx);
- }
- else
- {
- ICursor cursor = null;
- var docID = DocumentsContract.GetDocumentId(contentURI);
- var id = docID.Split(':')[1];
- var whereSelect = MediaStore.Images.ImageColumns.Id + "=?";
- var projections = new string[] { MediaStore.Images.ImageColumns.Data };
- cursor = ContentResolver.Query(MediaStore.Images.Media.InternalContentUri, projections, whereSelect, new string[] { id }, null);
- if (cursor.Count == 0)
- {
- cursor = ContentResolver.Query(MediaStore.Images.Media.ExternalContentUri, projections, whereSelect, new string[] { id }, null);
- }
- var colData = cursor.GetColumnIndexOrThrow(MediaStore.Images.ImageColumns.Data);
- cursor.MoveToFirst();
- fullPathToImage = cursor.GetString(colData);
- }
- return fullPathToImage;
- }
- catch (Exception ex)
- {
- Toast.MakeText(Xamarin.Forms.Forms.Context, "Unable to get path", ToastLength.Long).Show();
- }
- return null;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement