Last active
June 15, 2016 14:34
-
-
Save jzeferino/6ee837381ec1c78b383960026c9d784b to your computer and use it in GitHub Desktop.
A class that retrieve the path to a local resource (video/image/audio) in phone.
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
using System; | |
using Android.Content; | |
using Android.Database; | |
using Android.OS; | |
using Android.Provider; | |
using Java.Lang; | |
using Environment = Android.OS.Environment; | |
using String = System.String; | |
using Uri = Android.Net.Uri; | |
namespace PathUtils | |
{ | |
public static class PathUtils | |
{ | |
public static string GetPath (Context context, Uri uri) | |
{ | |
var isKitKat = Build.VERSION.SdkInt >= BuildVersionCodes.Kitkat; | |
// DocumentProvider | |
if (isKitKat && DocumentsContract.IsDocumentUri (context, uri)) { | |
// ExternalStorageProvider | |
if (IsExternalStorageDocument (uri)) { | |
var docId = DocumentsContract.GetDocumentId (uri); | |
var split = docId.Split (':'); | |
var type = split [0]; | |
if ("primary".EqualsIgnoreCase (type)) { | |
return Environment.ExternalStorageDirectory + "/" + split [1]; | |
} | |
} | |
// DownloadsProvider | |
else if (IsDownloadsDocument (uri)) { | |
var id = DocumentsContract.GetDocumentId (uri); | |
var contentUri = ContentUris.WithAppendedId ( | |
Uri.Parse ("content://downloads/public_downloads"), (long)Long.ValueOf (id)); | |
return GetDataColumn (context, contentUri, null, null); | |
} | |
// MediaProvider | |
else if (IsMediaDocument (uri)) { | |
var docId = DocumentsContract.GetDocumentId (uri); | |
var split = docId.Split (':'); | |
var type = split [0]; | |
Uri contentUri = null; | |
if ("image".Equals (type)) { | |
contentUri = MediaStore.Images.Media.ExternalContentUri; | |
} else if ("video".Equals (type)) { | |
contentUri = MediaStore.Video.Media.ExternalContentUri; | |
} else if ("audio".Equals (type)) { | |
contentUri = MediaStore.Audio.Media.ExternalContentUri; | |
} | |
var selection = "_id=?"; | |
var selectionArgs = new [] | |
{ | |
split[1] | |
}; | |
return GetDataColumn (context, contentUri, selection, selectionArgs); | |
} | |
} | |
// MediaStore (and general) | |
else if ("content".EqualsIgnoreCase (uri.Scheme)) { | |
return GetDataColumn (context, uri, null, null); | |
} | |
// File | |
else if ("file".EqualsIgnoreCase (uri.Scheme)) { | |
return uri.Path; | |
} | |
return null; | |
} | |
private static String GetDataColumn (Context context, Uri uri, String selection, | |
String [] selectionArgs) | |
{ | |
ICursor cursor = null; | |
var column = "_data"; | |
string [] projection = | |
{ | |
column | |
}; | |
try { | |
cursor = context.ContentResolver.Query (uri, projection, selection, selectionArgs, | |
null); | |
if (cursor != null && cursor.MoveToFirst ()) { | |
var columnIndex = cursor.GetColumnIndexOrThrow (column); | |
return cursor.GetString (columnIndex); | |
} | |
} finally { | |
if (cursor != null) | |
cursor.Close (); | |
} | |
return null; | |
} | |
/** | |
* @param uri The Uri to check. | |
* @return Whether the Uri authority is ExternalStorageProvider. | |
*/ | |
private static bool IsExternalStorageDocument (Uri uri) | |
{ | |
return "com.android.externalstorage.documents".Equals (uri.Authority); | |
} | |
/** | |
* @param uri The Uri to check. | |
* @return Whether the Uri authority is DownloadsProvider. | |
*/ | |
private static bool IsDownloadsDocument (Uri uri) | |
{ | |
return "com.android.providers.downloads.documents".Equals (uri.Authority); | |
} | |
/** | |
* @param uri The Uri to check. | |
* @return Whether the Uri authority is MediaProvider. | |
*/ | |
private static bool IsMediaDocument (Uri uri) | |
{ | |
return "com.android.providers.media.documents".Equals (uri.Authority); | |
} | |
private static bool EqualsIgnoreCase (this string first, string second) | |
{ | |
return String.Equals (second, first, StringComparison.OrdinalIgnoreCase); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment