Below are two solutions
1) You can use the code below. It can process files of any type and from any folder.
private String getPath(final Uri uri) { final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT; if(isKitKat) { // MediaStore (and general) return getForApi19(uri); } else if ("content".equalsIgnoreCase(uri.getScheme())) { // Return the remote address if (isGooglePhotosUri(uri)) return uri.getLastPathSegment(); return getDataColumn(uri, null, null); } // File else if ("file".equalsIgnoreCase(uri.getScheme())) { return uri.getPath(); } return null; } @TargetApi(19) private String getForApi19(Uri uri) { Log.e(tag, "+++ API 19 URI :: " + uri); if (DocumentsContract.isDocumentUri(this, uri)) { Log.e(tag, "+++ Document URI"); // ExternalStorageProvider if (isExternalStorageDocument(uri)) { Log.e(tag, "+++ External Document URI"); final String docId = DocumentsContract.getDocumentId(uri); final String[] split = docId.split(":"); final String type = split[0]; if ("primary".equalsIgnoreCase(type)) { Log.e(tag, "+++ Primary External Document URI"); return Environment.getExternalStorageDirectory() + "/" + split[1]; } // TODO handle non-primary volumes } // DownloadsProvider else if (isDownloadsDocument(uri)) { Log.e(tag, "+++ Downloads External Document URI"); final String id = DocumentsContract.getDocumentId(uri); final Uri contentUri = ContentUris.withAppendedId( Uri.parse("content://downloads/public_downloads"), Long.valueOf(id)); return getDataColumn(contentUri, null, null); } // MediaProvider else if (isMediaDocument(uri)) { Log.e(tag, "+++ Media Document URI"); final String docId = DocumentsContract.getDocumentId(uri); final String[] split = docId.split(":"); final String type = split[0]; Uri contentUri = null; if ("image".equals(type)) { Log.e(tag, "+++ Image Media Document URI"); contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; } else if ("video".equals(type)) { Log.e(tag, "+++ Video Media Document URI"); contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI; } else if ("audio".equals(type)) { Log.e(tag, "+++ Audio Media Document URI"); contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; } final String selection = "_id=?"; final String[] selectionArgs = new String[] { split[1] }; return getDataColumn(contentUri, selection, selectionArgs); } } else if ("content".equalsIgnoreCase(uri.getScheme())) { Log.e(tag, "+++ No DOCUMENT URI :: CONTENT "); // Return the remote address if (isGooglePhotosUri(uri)) return uri.getLastPathSegment(); return getDataColumn(uri, null, null); } // File else if ("file".equalsIgnoreCase(uri.getScheme())) { Log.e(tag, "+++ No DOCUMENT URI :: FILE "); return uri.getPath(); } return null; } /** * Get the value of the data column for this Uri. This is useful for * MediaStore Uris, and other file-based ContentProviders. * * @param uri The Uri to query. * @param selection (Optional) Filter used in the query. * @param selectionArgs (Optional) Selection arguments used in the query. * @return The value of the _data column, which is typically a file path. */ public String getDataColumn(Uri uri, String selection, String[] selectionArgs) { Cursor cursor = null; final String column = "_data"; final String[] projection = { column }; try { cursor = getContentResolver().query(uri, projection, selection, selectionArgs, null); if (cursor != null && cursor.moveToFirst()) { final int index = cursor.getColumnIndexOrThrow(column); return cursor.getString(index); } } finally { if (cursor != null) cursor.close(); } return null; } /** * @param uri The Uri to check. * @return Whether the Uri authority is ExternalStorageProvider. */ public static boolean isExternalStorageDocument(Uri uri) { return "com.android.externalstorage.documents".equals(uri.getAuthority()); } /** * @param uri The Uri to check. * @return Whether the Uri authority is DownloadsProvider. */ public static boolean isDownloadsDocument(Uri uri) { return "com.android.providers.downloads.documents".equals(uri.getAuthority()); } /** * @param uri The Uri to check. * @return Whether the Uri authority is MediaProvider. */ public static boolean isMediaDocument(Uri uri) { return "com.android.providers.media.documents".equals(uri.getAuthority()); } /** * @param uri The Uri to check. * @return Whether the Uri authority is Google Photos. */ public static boolean isGooglePhotosUri(Uri uri) { return "com.google.android.apps.photos.content".equals(uri.getAuthority()); }
This is the best solution for viewing files.
Hope this helps.
Another way
2) Another solution I found is
Add dependency to build.gradle
module: application
compile 'in.gauriinfotech:commons:1.0.8'
Then in your code use
String fullPath = Commons.getPath(uri, context);
Make sure you add permission below in Manifest.xml
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />