|
| 1 | +package com.androidx.utils; |
| 2 | + |
| 3 | +import android.annotation.SuppressLint; |
| 4 | +import android.content.ContentUris; |
| 5 | +import android.content.Context; |
| 6 | +import android.content.CursorLoader; |
| 7 | +import android.database.Cursor; |
| 8 | +import android.net.Uri; |
| 9 | +import android.os.Build; |
| 10 | +import android.os.Environment; |
| 11 | +import android.provider.DocumentsContract; |
| 12 | +import android.provider.MediaStore; |
| 13 | +import android.util.Log; |
| 14 | + |
| 15 | +import java.util.Locale; |
| 16 | + |
| 17 | +import androidx.annotation.RequiresApi; |
| 18 | + |
| 19 | +/** |
| 20 | + * Created by didik on 2016/12/10. |
| 21 | + */ |
| 22 | + |
| 23 | +class Uri2Path { |
| 24 | + private Uri2Path() { |
| 25 | + } |
| 26 | + |
| 27 | + @SuppressLint("NewApi") |
| 28 | + public static String getRealPathFromURI_API19(Context context, Uri uri) { |
| 29 | + return getPathSDK19(context, uri); |
| 30 | + } |
| 31 | + |
| 32 | + @SuppressLint("NewApi") |
| 33 | + public static String getRealPathFromURI_API11to18(Context context, Uri contentUri) { |
| 34 | + String[] proj = {MediaStore.Images.Media.DATA}; |
| 35 | + String result = null; |
| 36 | + CursorLoader cursorLoader = new CursorLoader( |
| 37 | + context, |
| 38 | + contentUri, proj, null, null, null); |
| 39 | + Cursor cursor = null; |
| 40 | + try { |
| 41 | + cursor = cursorLoader.loadInBackground(); |
| 42 | + if (cursor != null) { |
| 43 | + int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); |
| 44 | + cursor.moveToFirst(); |
| 45 | + result = cursor.getString(column_index); |
| 46 | + } |
| 47 | + } catch (IllegalArgumentException e) { |
| 48 | + e.printStackTrace(); |
| 49 | + } finally { |
| 50 | + if (cursor != null) { |
| 51 | + cursor.close(); |
| 52 | + } |
| 53 | + } |
| 54 | + return result; |
| 55 | + } |
| 56 | + |
| 57 | + public static String getRealPathFromURI_BelowAPI11(Context context, Uri contentUri) { |
| 58 | + String[] proj = {MediaStore.Images.Media.DATA}; |
| 59 | + String result = null; |
| 60 | + Cursor cursor = null; |
| 61 | + try { |
| 62 | + cursor = context.getContentResolver().query(contentUri, proj, null, null, null); |
| 63 | + int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); |
| 64 | + cursor.moveToFirst(); |
| 65 | + result = cursor.getString(column_index); |
| 66 | + } catch (Exception e) { |
| 67 | + e.printStackTrace(); |
| 68 | + } finally { |
| 69 | + if (cursor != null) { |
| 70 | + cursor.close(); |
| 71 | + } |
| 72 | + } |
| 73 | + return result; |
| 74 | + } |
| 75 | + |
| 76 | + @RequiresApi(api = Build.VERSION_CODES.KITKAT) |
| 77 | + private static String getPathSDK19(Context context, Uri uri) { |
| 78 | + // DocumentProvider |
| 79 | + if (DocumentsContract.isDocumentUri(context, uri)) { |
| 80 | + // ExternalStorageProvider |
| 81 | + if (isExternalStorageDocument(uri)) { |
| 82 | + final String docId = DocumentsContract.getDocumentId(uri); |
| 83 | + final String[] split = docId.split(":"); |
| 84 | + final String type = split[0]; |
| 85 | + |
| 86 | + if ("primary".equalsIgnoreCase(type)) { |
| 87 | + return Environment.getExternalStorageDirectory() + "/" + split[1]; |
| 88 | + } |
| 89 | + |
| 90 | + // TODO handle non-primary volumes |
| 91 | + } |
| 92 | + // DownloadsProvider |
| 93 | + else if (isDownloadsDocument(uri)) { |
| 94 | + |
| 95 | + final String id = DocumentsContract.getDocumentId(uri); |
| 96 | + final Uri contentUri = ContentUris.withAppendedId( |
| 97 | + Uri.parse("content://downloads/public_downloads"), Long.valueOf(id)); |
| 98 | + |
| 99 | + return getDataColumn(context, contentUri, null, null); |
| 100 | + } |
| 101 | + // MediaProvider |
| 102 | + else if (isMediaDocument(uri)) { |
| 103 | + final String docId = DocumentsContract.getDocumentId(uri); |
| 104 | + final String[] split = docId.split(":"); |
| 105 | + final String type = split[0]; |
| 106 | + |
| 107 | + Uri contentUri = null; |
| 108 | + if ("image".equals(type)) { |
| 109 | + contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; |
| 110 | + } else if ("video".equals(type)) { |
| 111 | + contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI; |
| 112 | + } else if ("audio".equals(type)) { |
| 113 | + contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; |
| 114 | + } |
| 115 | + |
| 116 | + final String selection = "_id=?"; |
| 117 | + final String[] selectionArgs = new String[]{ |
| 118 | + split[1] |
| 119 | + }; |
| 120 | + |
| 121 | + return getDataColumn(context, contentUri, selection, selectionArgs); |
| 122 | + } |
| 123 | + } |
| 124 | + // MediaStore (and general) |
| 125 | + else if ("content".equalsIgnoreCase(uri.getScheme())) { |
| 126 | + return getDataColumn(context, uri, null, null); |
| 127 | + } |
| 128 | + // File |
| 129 | + else if ("file".equalsIgnoreCase(uri.getScheme())) { |
| 130 | + return uri.getPath(); |
| 131 | + } |
| 132 | + |
| 133 | + return null; |
| 134 | + } |
| 135 | + |
| 136 | + |
| 137 | + /** |
| 138 | + * TAG for log messages. |
| 139 | + */ |
| 140 | + private static final String TAG = "FileUtils"; |
| 141 | + |
| 142 | + /** |
| 143 | + * @param uri The Uri to check. |
| 144 | + * @return Whether the Uri authority is ExternalStorageProvider. |
| 145 | + */ |
| 146 | + public static boolean isExternalStorageDocument(Uri uri) { |
| 147 | + return "com.android.externalstorage.documents".equals(uri.getAuthority()); |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * @param uri The Uri to check. |
| 152 | + * @return Whether the Uri authority is DownloadsProvider. |
| 153 | + */ |
| 154 | + public static boolean isDownloadsDocument(Uri uri) { |
| 155 | + return "com.android.providers.downloads.documents".equals(uri.getAuthority()); |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * @param uri The Uri to check. |
| 160 | + * @return Whether the Uri authority is MediaProvider. |
| 161 | + */ |
| 162 | + public static boolean isMediaDocument(Uri uri) { |
| 163 | + return "com.android.providers.media.documents".equals(uri.getAuthority()); |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * @param uri The Uri to check. |
| 168 | + * @return Whether the Uri authority is Google Photos. |
| 169 | + */ |
| 170 | + public static boolean isGooglePhotosUri(Uri uri) { |
| 171 | + return "com.google.android.apps.photos.content".equals(uri.getAuthority()); |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * Get the value of the data column for this Uri. This is useful for |
| 176 | + * MediaStore Uris, and other file-based ContentProviders. |
| 177 | + * |
| 178 | + * @param context The context. |
| 179 | + * @param uri The Uri to query. |
| 180 | + * @param selection (Optional) Filter used in the query. |
| 181 | + * @param selectionArgs (Optional) Selection arguments used in the query. |
| 182 | + * @return The value of the _data column, which is typically a file path. |
| 183 | + */ |
| 184 | + public static String getDataColumn(Context context, Uri uri, String selection, |
| 185 | + String[] selectionArgs) { |
| 186 | + |
| 187 | + Cursor cursor = null; |
| 188 | + final String column = "_data"; |
| 189 | + final String[] projection = {column}; |
| 190 | + |
| 191 | + try { |
| 192 | + cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null); |
| 193 | + if (cursor != null && cursor.moveToFirst()) { |
| 194 | + final int column_index = cursor.getColumnIndexOrThrow(column); |
| 195 | + return cursor.getString(column_index); |
| 196 | + } |
| 197 | + } catch (IllegalArgumentException ex) { |
| 198 | + Log.i(TAG, String.format(Locale.getDefault(), "getDataColumn: _data - [%s]", ex.getMessage())); |
| 199 | + } finally { |
| 200 | + if (cursor != null) { |
| 201 | + cursor.close(); |
| 202 | + } |
| 203 | + } |
| 204 | + return null; |
| 205 | + } |
| 206 | +} |
0 commit comments