Skip to content

Instantly share code, notes, and snippets.

@ryupold
Last active June 3, 2020 09:31
Show Gist options
  • Save ryupold/fe38e5acbe1586681e27 to your computer and use it in GitHub Desktop.
Save ryupold/fe38e5acbe1586681e27 to your computer and use it in GitHub Desktop.
Get file path from Content-URI in Android (Xamarin)
private string GetImageFromURI(Uri contentURI)
{
return MediaStore.Images.Media.GetBitmap(ContentResolver, contentURI);
}
private string GetRealPathFromURI(Uri contentURI)
{
ICursor cursor = ContentResolver.Query(contentURI, null, null, null, null);
cursor.MoveToFirst();
string documentId = cursor.GetString(0);
documentId = documentId.Split(':')[1];
cursor.Close();
cursor = ContentResolver.Query(
Android.Provider.MediaStore.Images.Media.ExternalContentUri,
null, MediaStore.Images.Media.InterfaceConsts.Id + " = ? ", new [] { documentId }, null);
cursor.MoveToFirst();
string path = cursor.GetString(cursor.GetColumnIndex(MediaStore.Images.Media.InterfaceConsts.Data));
cursor.Close();
return path;
}
@parthanjaria
Copy link

how do we get the path for a File for example any pdf or doc? from Uri?

@Gnanamprakash
Copy link

How to get path for a file for excel ??

@rhmndnt
Copy link

rhmndnt commented Jul 12, 2017

I got cursor null reference, do you have any suggestion?

@dvlaskin
Copy link

Works perfectly!
send {content://com.android.providers.media.documents/document/image%3A45}
get /storage/emulated/0/Download/20170720_105923.jpg

@eli191
Copy link

eli191 commented Jan 1, 2018

Hi, I wanted to use it, however I get an error : An object reference is required for ContentResolver.Query . Any help would be very much appreciated!
EDIT : My mistake! Since I am using this code inside a fragment, I just needed to use the activity context like this Activity.ContentResolver.Query and it works amazingly!!

@kiran75
Copy link

kiran75 commented Jun 14, 2018

How can take video file real path can any help me

@Rombersoft
Copy link

Ha ha! Your code is related to images files but what to do with any others files? Forexample, exe, db3, doc, pdf...

@ansari9546
Copy link

Ha ha! Your code is related to images files but what to do with any others files? Forexample, exe, db3, doc, pdf...

Me also looking for the same solution

@shira164
Copy link

shira164 commented Jun 3, 2020

Any solution for others files?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment