Advertisement
vergepuppeter

Utils.getPath

Apr 20th, 2017
1,255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.48 KB | None | 0 0
  1. public class Utils{
  2. public static String getActualPath(final Context context, final Uri uri) {
  3.  
  4.         final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
  5.  
  6.         // DocumentProvider
  7.         if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
  8.             // ExternalStorageProvider
  9.             if (isExternalStorageDocument(uri)) {
  10.                 final String docId = DocumentsContract.getDocumentId(uri);
  11.                 final String[] split = docId.split(":");
  12.                 final String type = split[0];
  13.  
  14.                 if ("primary".equalsIgnoreCase(type)) {
  15.                     return Environment.getExternalStorageDirectory() + "/" + split[1];
  16.                 }
  17.  
  18.                 // TODO handle non-primary volumes
  19.             }
  20.             // DownloadsProvider
  21.             else if (isDownloadsDocument(uri)) {
  22.  
  23.                 final String id = DocumentsContract.getDocumentId(uri);
  24.                 final Uri contentUri = ContentUris.withAppendedId(
  25.                         Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
  26.  
  27.                 return getDataColumn(context, contentUri, null, null);
  28.             }
  29.             // MediaProvider
  30.             else if (isMediaDocument(uri)) {
  31.                 final String docId = DocumentsContract.getDocumentId(uri);
  32.                 final String[] split = docId.split(":");
  33.                 final String type = split[0];
  34.  
  35.                 Uri contentUri = null;
  36.                 if ("image".equals(type)) {
  37.                     contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
  38.                 } else if ("video".equals(type)) {
  39.                     contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
  40.                 } else if ("audio".equals(type)) {
  41.                     contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
  42.                 }
  43.  
  44.                 final String selection = "_id=?";
  45.                 final String[] selectionArgs = new String[] {
  46.                         split[1]
  47.                 };
  48.  
  49.                 return getDataColumn(context, contentUri, selection, selectionArgs);
  50.             }
  51.         }
  52.         // MediaStore (and general)
  53.         else if ("content".equalsIgnoreCase(uri.getScheme())) {
  54.             return getDataColumn(context, uri, null, null);
  55.         }
  56.         // File
  57.         else if ("file".equalsIgnoreCase(uri.getScheme())) {
  58.             return uri.getPath();
  59.         }
  60.  
  61.         return null;
  62.     }
  63.  
  64.     /**
  65.      * Get the value of the data column for this Uri. This is useful for
  66.      * MediaStore Uris, and other file-based ContentProviders.
  67.      *
  68.      * @param context The context.
  69.      * @param uri The Uri to query.
  70.      * @param selection (Optional) Filter used in the query.
  71.      * @param selectionArgs (Optional) Selection arguments used in the query.
  72.      * @return The value of the _data column, which is typically a file path.
  73.      */
  74.     private static String getDataColumn(Context context, Uri uri, String selection,
  75.                                        String[] selectionArgs) {
  76.  
  77.         Cursor cursor = null;
  78.         final String column = "_data";
  79.         final String[] projection = {
  80.                 column
  81.         };
  82.  
  83.         try {
  84.             cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
  85.                     null);
  86.             if (cursor != null && cursor.moveToFirst()) {
  87.                 final int column_index = cursor.getColumnIndexOrThrow(column);
  88.                 return cursor.getString(column_index);
  89.             }
  90.         } finally {
  91.             if (cursor != null)
  92.                 cursor.close();
  93.         }
  94.         return null;
  95.     }
  96.  
  97.  
  98.     /**
  99.      * @param uri The Uri to check.
  100.      * @return Whether the Uri authority is ExternalStorageProvider.
  101.      */
  102.     public static boolean isExternalStorageDocument(Uri uri) {
  103.         return "com.android.externalstorage.documents".equals(uri.getAuthority());
  104.     }
  105.  
  106.     /**
  107.      * @param uri The Uri to check.
  108.      * @return Whether the Uri authority is DownloadsProvider.
  109.      */
  110.     public static boolean isDownloadsDocument(Uri uri) {
  111.         return "com.android.providers.downloads.documents".equals(uri.getAuthority());
  112.     }
  113.  
  114.     /**
  115.      * @param uri The Uri to check.
  116.      * @return Whether the Uri authority is MediaProvider.
  117.      */
  118.     public static boolean isMediaDocument(Uri uri) {
  119.         return "com.android.providers.media.documents".equals(uri.getAuthority());
  120.     }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement