Skip to content

Instantly share code, notes, and snippets.

@steveevers
Last active November 15, 2017 22:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save steveevers/a5af24c226f44bb8fdc3 to your computer and use it in GitHub Desktop.
Save steveevers/a5af24c226f44bb8fdc3 to your computer and use it in GitHub Desktop.
From an answer posted here: http://stackoverflow.com/a/20559175 converted to Xamarin.Android
public static class DocumentHelper {
public static string GetPathToFile(Context context, Android.Net.Uri uri) {
bool isKitkat = Build.VERSION.SdkInt >= BuildVersionCodes.Kitkat;
if (isKitkat && DocumentsContract.IsDocumentUri(context, uri)) {
if (uri.IsExternalStorageDocument()) {
var parts = DocumentsContract.GetDocumentId(uri).Split(':');
var type = parts[0];
var name = parts[1];
if (type.Equals("primary", StringComparison.InvariantCultureIgnoreCase)) {
return string.Join("/", Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, name);
}
} else if (uri.IsDownloadsDocument()) {
var id = DocumentsContract.GetDocumentId(uri);
var contentUri = ContentUris.WithAppendedId(Android.Net.Uri.Parse("content://downloads/public_downloads"), long.Parse(id));
return GetDataColumn(context, contentUri, null, null);
} else if (uri.IsMediaDocument()) {
var parts = DocumentsContract.GetDocumentId(uri).Split(':');
var type = parts[0];
var name = parts[1];
Android.Net.Uri contentUri = GetContentUri(type);
var selection = "_id=?";
var selectionArgs = new string[] { name };
return GetDataColumn(context, contentUri, selection, selectionArgs) ?? string.Empty;
}
} else if (uri.Scheme.Equals("content", StringComparison.InvariantCultureIgnoreCase)) {
return GetDataColumn(context, uri, null, null) ?? string.Empty;
} else if (uri.Scheme.Equals("file", StringComparison.InvariantCultureIgnoreCase)) {
return uri.Path;
}
return string.Empty;
}
private static Android.Net.Uri GetContentUri(string type) {
switch (type) {
case "image":
return MediaStore.Images.Media.ExternalContentUri;
case "video":
return MediaStore.Video.Media.ExternalContentUri;
case "audio":
return MediaStore.Audio.Media.ExternalContentUri;
default:
return null;
}
}
private static string GetDataColumn(Context context, Android.Net.Uri uri, string selection, string[] selectionArgs) {
var column = "_data";
var projection = new string[] { column };
using (var cursor = context.ContentResolver.Query(uri, projection, selection, selectionArgs, null)) {
if (cursor != null && cursor.MoveToFirst()) {
return cursor.GetString(cursor.GetColumnIndexOrThrow(column));
}
}
return null;
}
public static bool IsExternalStorageDocument(this Android.Net.Uri uri) {
return uri.Authority == "com.android.externalstorage.documents";
}
public static bool IsDownloadsDocument(this Android.Net.Uri uri) {
return uri.Authority == "com.android.providers.downloads.documents";
}
public static bool IsMediaDocument(this Android.Net.Uri uri) {
return uri.Authority == "com.android.providers.media.documents";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment