Last active
June 3, 2020 09:31
-
-
Save ryupold/fe38e5acbe1586681e27 to your computer and use it in GitHub Desktop.
Get file path from Content-URI in Android (Xamarin)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private string GetImageFromURI(Uri contentURI) | |
{ | |
return MediaStore.Images.Media.GetBitmap(ContentResolver, contentURI); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
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!!
How can take video file real path can any help me
Ha ha! Your code is related to images files but what to do with any others files? Forexample, exe, db3, doc, pdf...
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
Any solution for others files?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Works perfectly!
send {content://com.android.providers.media.documents/document/image%3A45}
get /storage/emulated/0/Download/20170720_105923.jpg