|
| 1 | +package com.amit.dbapilibrary.utilities; |
| 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.Build; |
| 8 | +import android.os.Environment; |
| 9 | +import android.provider.DocumentsContract; |
| 10 | +import android.provider.MediaStore; |
| 11 | + |
| 12 | +/** |
| 13 | + * Created by AmitS on 15-05-2017. |
| 14 | + */ |
| 15 | + |
| 16 | +public class FilePath |
| 17 | +{ |
| 18 | + /** |
| 19 | + * Method for return file path of Gallery image/ Document / Video / Audio |
| 20 | + * |
| 21 | + * @param context |
| 22 | + * @param uri |
| 23 | + * @return - path of the selected image file from gallery |
| 24 | + */ |
| 25 | + public static String getPath(final Context context, final Uri uri) |
| 26 | + { |
| 27 | + // check here to KITKAT or new version |
| 28 | + final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT; |
| 29 | + |
| 30 | + // DocumentProvider |
| 31 | + if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) |
| 32 | + { |
| 33 | + // ExternalStorageProvider |
| 34 | + if (isExternalStorageDocument(uri)) |
| 35 | + { |
| 36 | + final String docId = DocumentsContract.getDocumentId(uri); |
| 37 | + final String[] split = docId.split(":"); |
| 38 | + final String type = split[0]; |
| 39 | + |
| 40 | + if ("primary".equalsIgnoreCase(type)) |
| 41 | + { |
| 42 | + return Environment.getExternalStorageDirectory() + "/" |
| 43 | + + split[1]; |
| 44 | + } |
| 45 | + } |
| 46 | + // DownloadsProvider |
| 47 | + else if (isDownloadsDocument(uri)) |
| 48 | + { |
| 49 | + final String id = DocumentsContract.getDocumentId(uri); |
| 50 | + |
| 51 | + final Uri contentUri = ContentUris.withAppendedId( |
| 52 | + Uri.parse("content://downloads/public_downloads"), |
| 53 | + Long.valueOf(id)); |
| 54 | + |
| 55 | + return getDataColumn(context, contentUri, null, null); |
| 56 | + } |
| 57 | + // MediaProvider |
| 58 | + else if (isMediaDocument(uri)) |
| 59 | + { |
| 60 | + final String docId = DocumentsContract.getDocumentId(uri); |
| 61 | + final String[] split = docId.split(":"); |
| 62 | + final String type = split[0]; |
| 63 | + |
| 64 | + Uri contentUri = null; |
| 65 | + |
| 66 | + if ("image".equals(type)) |
| 67 | + { |
| 68 | + contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; |
| 69 | + } |
| 70 | + else if ("video".equals(type)) |
| 71 | + { |
| 72 | + contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI; |
| 73 | + } |
| 74 | + else if ("audio".equals(type)) |
| 75 | + { |
| 76 | + contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; |
| 77 | + } |
| 78 | + |
| 79 | + final String selection = "_id=?"; |
| 80 | + final String[] selectionArgs = new String[] { split[1] }; |
| 81 | + |
| 82 | + return getDataColumn(context, contentUri, selection, |
| 83 | + selectionArgs); |
| 84 | + } |
| 85 | + } |
| 86 | + // MediaStore (and general) |
| 87 | + else if ("content".equalsIgnoreCase(uri.getScheme())) |
| 88 | + { |
| 89 | + // Return the remote address |
| 90 | + if (isGooglePhotosUri(uri)) |
| 91 | + return uri.getLastPathSegment(); |
| 92 | + |
| 93 | + return getDataColumn(context, uri, null, null); |
| 94 | + } |
| 95 | + // File |
| 96 | + else if ("file".equalsIgnoreCase(uri.getScheme())) |
| 97 | + { |
| 98 | + return uri.getPath(); |
| 99 | + } |
| 100 | + |
| 101 | + return null; |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Get the value of the data column for this Uri. This is useful for |
| 106 | + * MediaStore Uris, and other file-based ContentProviders. |
| 107 | + * |
| 108 | + * @param context - The context. |
| 109 | + * @param uri - The Uri to query. |
| 110 | + * @param selection - (Optional) Filter used in the query. |
| 111 | + * @param selectionArgs - (Optional) Selection arguments used in the query. |
| 112 | + * @return - The value of the _data column, which is typically a file path. |
| 113 | + */ |
| 114 | + private static String getDataColumn(Context context, Uri uri, |
| 115 | + String selection, String[] selectionArgs) |
| 116 | + { |
| 117 | + Cursor cursor = null; |
| 118 | + final String column = "_data"; |
| 119 | + final String[] projection = { column }; |
| 120 | + |
| 121 | + try |
| 122 | + { |
| 123 | + cursor = context.getContentResolver().query(uri, projection, |
| 124 | + selection, selectionArgs, null); |
| 125 | + |
| 126 | + if (cursor != null && cursor.moveToFirst()) |
| 127 | + { |
| 128 | + final int index = cursor.getColumnIndexOrThrow(column); |
| 129 | + return cursor.getString(index); |
| 130 | + } |
| 131 | + } |
| 132 | + finally |
| 133 | + { |
| 134 | + if (cursor != null) |
| 135 | + cursor.close(); |
| 136 | + } |
| 137 | + |
| 138 | + return null; |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * @param uri - The Uri to check. |
| 143 | + * @return - Whether the Uri authority is ExternalStorageProvider. |
| 144 | + */ |
| 145 | + private static boolean isExternalStorageDocument(Uri uri) |
| 146 | + { |
| 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 | + private static boolean isDownloadsDocument(Uri uri) |
| 155 | + { |
| 156 | + return "com.android.providers.downloads.documents".equals(uri.getAuthority()); |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * @param uri - The Uri to check. |
| 161 | + * @return - Whether the Uri authority is MediaProvider. |
| 162 | + */ |
| 163 | + private static boolean isMediaDocument(Uri uri) |
| 164 | + { |
| 165 | + return "com.android.providers.media.documents".equals(uri.getAuthority()); |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * @param uri - The Uri to check. |
| 170 | + * @return - Whether the Uri authority is Google Photos. |
| 171 | + */ |
| 172 | + private static boolean isGooglePhotosUri(Uri uri) |
| 173 | + { |
| 174 | + return "com.google.android.apps.photos.content".equals(uri.getAuthority()); |
| 175 | + } |
| 176 | +} |
0 commit comments