└── RealFilePathUtil.java /RealFilePathUtil.java: -------------------------------------------------------------------------------- 1 | package com.technophilegeek.myapplication; 2 | 3 | import android.annotation.SuppressLint; 4 | import android.content.ContentUris; 5 | import android.content.Context; 6 | import android.database.Cursor; 7 | import android.net.Uri; 8 | import android.os.Build; 9 | import android.os.Environment; 10 | import android.provider.DocumentsContract; 11 | import android.provider.MediaStore; 12 | import android.support.annotation.RequiresApi; 13 | import android.text.TextUtils; 14 | 15 | /** 16 | * Created by rjt on 28/11/18. 17 | */ 18 | 19 | public class RealFilePathUtil { 20 | @SuppressLint("NewApi") 21 | public static String getPath(final Context context, final Uri uri) { 22 | //check for KITKAT or above 23 | final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT; 24 | 25 | // DocumentProvider 26 | if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) { 27 | // ExternalStorageProvider 28 | if (isExternalStorageDocument(uri)) { 29 | final String docId = DocumentsContract.getDocumentId(uri); 30 | final String[] split = docId.split(":"); 31 | final String type = split[0]; 32 | 33 | if ("primary".equalsIgnoreCase(type)) { 34 | return Environment.getExternalStorageDirectory() + "/" + split[1]; 35 | } 36 | } 37 | // DownloadsProvider 38 | else if (isDownloadsDocument(uri)) { 39 | return getDownloadFilePath(context, uri); 40 | } 41 | // MediaProvider 42 | else if (isMediaDocument(uri)) { 43 | final String docId = DocumentsContract.getDocumentId(uri); 44 | final String[] split = docId.split(":"); 45 | final String type = split[0]; 46 | 47 | Uri contentUri = null; 48 | if ("image".equals(type)) { 49 | contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; 50 | } else if ("video".equals(type)) { 51 | contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI; 52 | } else if ("audio".equals(type)) { 53 | contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; 54 | } 55 | 56 | final String selection = "_id=?"; 57 | final String[] selectionArgs = new String[] { 58 | split[1] 59 | }; 60 | 61 | return getDataColumn(context, contentUri, selection, selectionArgs); 62 | } 63 | } 64 | 65 | // MediaStore (and general) 66 | else if ("content".equalsIgnoreCase(uri.getScheme())) { 67 | // Return the remote address 68 | if (isGooglePhotosUri(uri)) 69 | return uri.getLastPathSegment(); 70 | 71 | return getDataColumn(context, uri, null, null); 72 | } 73 | // File 74 | else if ("file".equalsIgnoreCase(uri.getScheme())) { 75 | return uri.getPath(); 76 | } 77 | 78 | return null; 79 | } 80 | 81 | public static String getDataColumn(Context context, Uri uri, String selection, 82 | String[] selectionArgs) { 83 | 84 | Cursor cursor = null; 85 | final String column = "_data"; 86 | final String[] projection = { 87 | column 88 | }; 89 | try { 90 | cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, 91 | null); 92 | if (cursor != null && cursor.moveToFirst()) { 93 | final int index = cursor.getColumnIndexOrThrow(column); 94 | return cursor.getString(index); 95 | } 96 | } finally { 97 | if (cursor != null) 98 | cursor.close(); 99 | } 100 | return null; 101 | } 102 | 103 | @RequiresApi(api = Build.VERSION_CODES.KITKAT) 104 | public static String getDownloadFilePath(Context context, Uri uri){ 105 | Cursor cursor = null; 106 | final String[] projection = { 107 | MediaStore.MediaColumns.DISPLAY_NAME 108 | }; 109 | try { 110 | cursor = context.getContentResolver().query(uri, projection, null, null, null); 111 | if (cursor != null && cursor.moveToFirst()) { 112 | String fileName=cursor.getString(0); 113 | String path =Environment.getExternalStorageDirectory().toString() + "/Download/" + fileName; 114 | if (!TextUtils.isEmpty(path)){ 115 | return path; 116 | } 117 | } 118 | } finally { 119 | cursor.close(); 120 | } 121 | String id = DocumentsContract.getDocumentId(uri); 122 | if (id.startsWith("raw:")) { 123 | return id.replaceFirst("raw:", ""); 124 | } 125 | Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads"), java.lang.Long.valueOf(id)); 126 | 127 | return getDataColumn(context, contentUri, null, null); 128 | } 129 | 130 | 131 | public static boolean isExternalStorageDocument(Uri uri) { 132 | return "com.android.externalstorage.documents".equals(uri.getAuthority()); 133 | } 134 | 135 | public static boolean isDownloadsDocument(Uri uri) { 136 | return "com.android.providers.downloads.documents".equals(uri.getAuthority()); 137 | } 138 | 139 | public static boolean isMediaDocument(Uri uri) { 140 | return "com.android.providers.media.documents".equals(uri.getAuthority()); 141 | } 142 | 143 | public static boolean isGooglePhotosUri(Uri uri) { 144 | return "com.google.android.apps.photos.content".equals(uri.getAuthority()); 145 | } 146 | } 147 | --------------------------------------------------------------------------------