uri to filepath 2c get file path from uri

Solutions on MaxInterview for uri to filepath 2c get file path from uri by the best coders in the world

showing results for - "uri to filepath 2c get file path from uri"
Giorgio
25 Jan 2017
1public static String getPathFromUri(final Context context, final Uri uri) {
2
3        final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
4
5        // DocumentProvider
6        if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
7            // ExternalStorageProvider
8            if (isExternalStorageDocument(uri)) {
9                final String docId = DocumentsContract.getDocumentId(uri);
10                final String[] split = docId.split(":");
11                final String type = split[0];
12
13                if ("primary".equalsIgnoreCase(type)) {
14                    return Environment.getExternalStorageDirectory() + "/" + split[1];
15                }
16
17                // TODO handle non-primary volumes
18            }
19            // DownloadsProvider
20            else if (isDownloadsDocument(uri)) {
21
22                final String id = DocumentsContract.getDocumentId(uri);
23                final Uri contentUri = ContentUris.withAppendedId(
24                        Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
25
26                return getDataColumn(context, contentUri, null, null);
27            }
28            // MediaProvider
29            else if (isMediaDocument(uri)) {
30                final String docId = DocumentsContract.getDocumentId(uri);
31                final String[] split = docId.split(":");
32                final String type = split[0];
33
34                Uri contentUri = null;
35                if ("image".equals(type)) {
36                    contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
37                } else if ("video".equals(type)) {
38                    contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
39                } else if ("audio".equals(type)) {
40                    contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
41                }
42
43                final String selection = "_id=?";
44                final String[] selectionArgs = new String[] {
45                        split[1]
46                };
47
48                return getDataColumn(context, contentUri, selection, selectionArgs);
49            }
50        }
51        // MediaStore (and general)
52        else if ("content".equalsIgnoreCase(uri.getScheme())) {
53
54            // Return the remote address
55            if (isGooglePhotosUri(uri))
56                return uri.getLastPathSegment();
57
58            return getDataColumn(context, uri, null, null);
59        }
60        // File
61        else if ("file".equalsIgnoreCase(uri.getScheme())) {
62            return uri.getPath();
63        }
64
65        return null;
66    }
67
68    public static String getDataColumn(Context context, Uri uri, String selection,
69                                       String[] selectionArgs) {
70
71        Cursor cursor = null;
72        final String column = "_data";
73        final String[] projection = {
74                column
75        };
76
77        try {
78            cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
79                    null);
80            if (cursor != null && cursor.moveToFirst()) {
81                final int index = cursor.getColumnIndexOrThrow(column);
82                return cursor.getString(index);
83            }
84        } finally {
85            if (cursor != null)
86                cursor.close();
87        }
88        return null;
89    }
90
91
92    /**
93     * @param uri The Uri to check.
94     * @return Whether the Uri authority is ExternalStorageProvider.
95     */
96    public static boolean isExternalStorageDocument(Uri uri) {
97        return "com.android.externalstorage.documents".equals(uri.getAuthority());
98    }
99
100    /**
101     * @param uri The Uri to check.
102     * @return Whether the Uri authority is DownloadsProvider.
103     */
104    public static boolean isDownloadsDocument(Uri uri) {
105        return "com.android.providers.downloads.documents".equals(uri.getAuthority());
106    }
107
108    /**
109     * @param uri The Uri to check.
110     * @return Whether the Uri authority is MediaProvider.
111     */
112    public static boolean isMediaDocument(Uri uri) {
113        return "com.android.providers.media.documents".equals(uri.getAuthority());
114    }
115
116    /**
117     * @param uri The Uri to check.
118     * @return Whether the Uri authority is Google Photos.
119     */
120    public static boolean isGooglePhotosUri(Uri uri) {
121        return "com.google.android.apps.photos.content".equals(uri.getAuthority());
122}
123
similar questions
queries leading to this page
uri to filepath 2c get file path from uri