|
| 1 | +package protect.videotranscoder; |
| 2 | + |
| 3 | +import android.content.ContentUris; |
| 4 | +import android.content.Context; |
| 5 | +import android.database.Cursor; |
| 6 | +import android.net.Uri; |
| 7 | +import android.os.Environment; |
| 8 | +import android.provider.DocumentsContract; |
| 9 | +import android.provider.MediaStore; |
| 10 | + |
| 11 | +public class FileUtil |
| 12 | +{ |
| 13 | + /** |
| 14 | + * Get a file path from a Uri. This will get the the path for Storage Access |
| 15 | + * Framework Documents, as well as the _data field for the MediaStore and |
| 16 | + * other file-based ContentProviders. |
| 17 | + */ |
| 18 | + public static String getPath(final Context context, final Uri uri) |
| 19 | + { |
| 20 | + // DocumentProvider |
| 21 | + if (DocumentsContract.isDocumentUri(context, uri)) |
| 22 | + { |
| 23 | + // ExternalStorageProvider |
| 24 | + if (isExternalStorageDocument(uri)) |
| 25 | + { |
| 26 | + final String docId = DocumentsContract.getDocumentId(uri); |
| 27 | + final String[] split = docId.split(":"); |
| 28 | + final String type = split[0]; |
| 29 | + |
| 30 | + if ("primary".equalsIgnoreCase(type)) |
| 31 | + { |
| 32 | + return Environment.getExternalStorageDirectory() + "/" + split[1]; |
| 33 | + } |
| 34 | + |
| 35 | + // TODO handle non-primary volumes |
| 36 | + } |
| 37 | + // DownloadsProvider |
| 38 | + else if (isDownloadsDocument(uri)) |
| 39 | + { |
| 40 | + final String id = DocumentsContract.getDocumentId(uri); |
| 41 | + final Uri contentUri = ContentUris.withAppendedId( |
| 42 | + Uri.parse("content://downloads/public_downloads"), Long.parseLong(id)); |
| 43 | + |
| 44 | + return getDataColumn(context, contentUri, null, null); |
| 45 | + } |
| 46 | + // MediaProvider |
| 47 | + else if (isMediaDocument(uri)) |
| 48 | + { |
| 49 | + final String docId = DocumentsContract.getDocumentId(uri); |
| 50 | + final String[] split = docId.split(":"); |
| 51 | + final String type = split[0]; |
| 52 | + |
| 53 | + Uri contentUri = null; |
| 54 | + if ("image".equals(type)) |
| 55 | + { |
| 56 | + contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; |
| 57 | + } |
| 58 | + else if ("video".equals(type)) |
| 59 | + { |
| 60 | + contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI; |
| 61 | + } |
| 62 | + else if ("audio".equals(type)) |
| 63 | + { |
| 64 | + contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; |
| 65 | + } |
| 66 | + |
| 67 | + final String selection = "_id=?"; |
| 68 | + final String[] selectionArgs = new String[] |
| 69 | + { |
| 70 | + split[1] |
| 71 | + }; |
| 72 | + |
| 73 | + return getDataColumn(context, contentUri, selection, selectionArgs); |
| 74 | + } |
| 75 | + } |
| 76 | + // MediaStore (and general) |
| 77 | + else if ("content".equalsIgnoreCase(uri.getScheme())) |
| 78 | + { |
| 79 | + return getDataColumn(context, uri, null, null); |
| 80 | + } |
| 81 | + // File |
| 82 | + else if ("file".equalsIgnoreCase(uri.getScheme())) |
| 83 | + { |
| 84 | + return uri.getPath(); |
| 85 | + } |
| 86 | + |
| 87 | + return null; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Get the value of the data column for this Uri. |
| 92 | + */ |
| 93 | + private static String getDataColumn(Context context, Uri uri, String selection, |
| 94 | + String[] selectionArgs) |
| 95 | + { |
| 96 | + Cursor cursor = null; |
| 97 | + final String column = "_data"; |
| 98 | + final String[] projection = |
| 99 | + { |
| 100 | + column |
| 101 | + }; |
| 102 | + |
| 103 | + try |
| 104 | + { |
| 105 | + cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, |
| 106 | + null); |
| 107 | + if (cursor != null && cursor.moveToFirst()) |
| 108 | + { |
| 109 | + final int column_index = cursor.getColumnIndexOrThrow(column); |
| 110 | + return cursor.getString(column_index); |
| 111 | + } |
| 112 | + } |
| 113 | + finally |
| 114 | + { |
| 115 | + if (cursor != null) |
| 116 | + { |
| 117 | + cursor.close(); |
| 118 | + } |
| 119 | + } |
| 120 | + return null; |
| 121 | + } |
| 122 | + |
| 123 | + |
| 124 | + /** |
| 125 | + * @param uri The Uri to check. |
| 126 | + * @return Whether the Uri authority is ExternalStorageProvider. |
| 127 | + */ |
| 128 | + private static boolean isExternalStorageDocument(Uri uri) |
| 129 | + { |
| 130 | + return "com.android.externalstorage.documents".equals(uri.getAuthority()); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * @param uri The Uri to check. |
| 135 | + * @return Whether the Uri authority is DownloadsProvider. |
| 136 | + */ |
| 137 | + private static boolean isDownloadsDocument(Uri uri) |
| 138 | + { |
| 139 | + return "com.android.providers.downloads.documents".equals(uri.getAuthority()); |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * @param uri The Uri to check. |
| 144 | + * @return Whether the Uri authority is MediaProvider. |
| 145 | + */ |
| 146 | + private static boolean isMediaDocument(Uri uri) |
| 147 | + { |
| 148 | + return "com.android.providers.media.documents".equals(uri.getAuthority()); |
| 149 | + } |
| 150 | +} |
0 commit comments