{"id":236,"date":"2020-03-20T11:46:02","date_gmt":"2020-03-20T03:46:02","guid":{"rendered":"https:\/\/badgameshow.com\/fly\/?p=236"},"modified":"2020-05-19T14:27:31","modified_gmt":"2020-05-19T06:27:31","slug":"util-uri-content-%e8%bd%89%e6%88%90file","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/fly\/util-uri-content-%e8%bd%89%e6%88%90file\/","title":{"rendered":"Util Uri content:\/\/ \u8f49\u6210file:\/\/"},"content":{"rendered":"<pre><code class=\"language-Java line-numbers\">import android.content.ContentResolver;\nimport android.content.ContentUris;\nimport android.content.Context;\nimport android.database.Cursor;\nimport android.net.Uri;\nimport android.os.Build;\nimport android.os.Environment;\nimport android.provider.DocumentsContract;\nimport android.provider.MediaStore;\n\npublic class GetFilePathByUriUtil {\n    public static String getFilePathByUri(Context context, Uri uri) {\n        String path = null;\n        \/\/ \u4ee5 file:\/\/ \u958b\u982d\u7684\n        if (ContentResolver.SCHEME_FILE.equals(uri.getScheme())) {\n            path = uri.getPath();\n            return path;\n        }\n        \/\/ \u4ee5 content:\/\/ \u958b\u982d\u7684\n        if (ContentResolver.SCHEME_CONTENT.equals(uri.getScheme()) &amp;&amp; Build.VERSION.SDK_INT &lt; Build.VERSION_CODES.KITKAT) {\n            Cursor cursor = context.getContentResolver().query(uri, new String[]{MediaStore.Images.Media.DATA}, null, null, null);\n            if (cursor != null) {\n                if (cursor.moveToFirst()) {\n                    int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);\n                    if (columnIndex &gt; -1) {\n                        path = cursor.getString(columnIndex);\n                    }\n                }\n                cursor.close();\n            }\n            return path;\n        }\n        \/\/ 4.4\u4e4b\u5f8c \u662f\u4ee5 content:\/\/ \u958b\u982d\u7684\n        if (ContentResolver.SCHEME_CONTENT.equals(uri.getScheme()) &amp;&amp; Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.KITKAT) {\n            if (DocumentsContract.isDocumentUri(context, uri)) {\n                if (isExternalStorageDocument(uri)) {\n                    \/\/ ExternalStorageProvider\n                    final String docId = DocumentsContract.getDocumentId(uri);\n                    final String[] split = docId.split(\":\");\n                    final String type = split[0];\n                    if (\"primary\".equalsIgnoreCase(type)) {\n                        path = Environment.getExternalStorageDirectory() + \"\/\" + split[1];\n                        return path;\n                    }\n                } else if (isDownloadsDocument(uri)) {\n                    \/\/ DownloadsProvider\n                    final String id = DocumentsContract.getDocumentId(uri);\n                    final Uri contentUri = ContentUris.withAppendedId(Uri.parse(\"content:\/\/downloads\/public_downloads\"),\n                            Long.valueOf(id));\n                    path = getDataColumn(context, contentUri, null, null);\n                    return path;\n                } else if (isMediaDocument(uri)) {\n                    \/\/ MediaProvider\n                    final String docId = DocumentsContract.getDocumentId(uri);\n                    final String[] split = docId.split(\":\");\n                    final String type = split[0];\n                    Uri contentUri = null;\n                    if (\"image\".equals(type)) {\n                        contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;\n                    } else if (\"video\".equals(type)) {\n                        contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;\n                    } else if (\"audio\".equals(type)) {\n                        contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;\n                    }\n                    final String selection = \"_id=?\";\n                    final String[] selectionArgs = new String[]{split[1]};\n                    path = getDataColumn(context, contentUri, selection, selectionArgs);\n                    return path;\n                }\n            }\n        }\n        return null;\n    }\n\n    private static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {\n        Cursor cursor = null;\n        final String column = \"_data\";\n        final String[] projection = {column};\n        try {\n            cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);\n            if (cursor != null &amp;&amp; cursor.moveToFirst()) {\n                final int column_index = cursor.getColumnIndexOrThrow(column);\n                return cursor.getString(column_index);\n            }\n        } finally {\n            if (cursor != null)\n                cursor.close();\n        }\n        return null;\n    }\n\n    private static boolean isExternalStorageDocument(Uri uri) {\n        return \"com.android.externalstorage.documents\".equals(uri.getAuthority());\n    }\n\n    private static boolean isDownloadsDocument(Uri uri) {\n        return \"com.android.providers.downloads.documents\".equals(uri.getAuthority());\n    }\n\n    private static boolean isMediaDocument(Uri uri) {\n        return \"com.android.providers.media.documents\".equals(uri.getAuthority());\n    }\n}\n\n<\/code><\/pre>\n\n<div style=\"font-size: 0px; height: 0px; line-height: 0px; margin: 0; padding: 0; clear: both;\"><\/div>","protected":false},"excerpt":{"rendered":"<p>import android.content.ContentResolver; import android. &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"pgc_sgb_lightbox_settings":"","footnotes":""},"categories":[9],"tags":[13,14,16],"class_list":["post-236","post","type-post","status-publish","format-standard","hentry","category-util","tag-android","tag-java","tag-util"],"_links":{"self":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/236","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/comments?post=236"}],"version-history":[{"count":2,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/236\/revisions"}],"predecessor-version":[{"id":486,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/236\/revisions\/486"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/media?parent=236"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/categories?post=236"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/tags?post=236"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}