转自知乎Matisse
package com.zhihu.matisse.internal.utils; import android.annotation.TargetApi; import android.content.ContentUris; import android.content.Context; import android.database.Cursor; import android.net.Uri; import android.os.Build; import android.os.Environment; import android.provider.DocumentsContract; import android.provider.MediaStore; /** * http://stackoverflow.com/a/27271131/4739220 */ public class PathUtils { /** * Get a file path from a Uri. This will get the the path for Storage Access * Framework Documents, as well as the _data field for the MediaStore and * other file-based ContentProviders. * * @param context The context. * @param uri The Uri to query. * @author paulburke */ @TargetApi(Build.VERSION_CODES.KITKAT) public static String getPath(final Context context, final Uri uri) { // DocumentProvider if (Platform.hasKitKat() && DocumentsContract.isDocumentUri(context, uri)) { // ExternalStorageProvider if (isExternalStorageDocument(uri)) { final String docId = DocumentsContract.getDocumentId(uri); final String[] split = docId.split(":"); final String type = split[0]; if ("primary".equalsIgnoreCase(type)) { return Environment.getExternalStorageDirectory() + "/" + split[1]; } // TODO handle non-primary volumes } else if (isDownloadsDocument(uri)) { // DownloadsProvider final String id = DocumentsContract.getDocumentId(uri); final Uri contentUri = ContentUris.withAppendedId( Uri.parse("content://downloads/public_downloads"), Long.valueOf(id)); return getDataColumn(context, contentUri, null, null); } else if (isMediaDocument(uri)) { // MediaProvider final String docId = DocumentsContract.getDocumentId(uri); final String[] split = docId.split(":"); final String type = split[0]; Uri contentUri = null; if ("image".equals(type)) { contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; } else if ("video".equals(type)) { contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI; } else if ("audio".equals(type)) { contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; } final String selection = "_id=?"; final String[] selectionArgs = new String[]{ split[1] }; return getDataColumn(context, contentUri, selection, selectionArgs); } } else if ("content".equalsIgnoreCase(uri.getScheme())) { // MediaStore (and general) return getDataColumn(context, uri, null, null); } else if ("file".equalsIgnoreCase(uri.getScheme())) { // 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 context The context. * @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 static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) { Cursor cursor = null; final String column = "_data"; final String[] projection = { column }; try { cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null); if (cursor != null && cursor.moveToFirst()) { final int columnIndex = cursor.getColumnIndexOrThrow(column); return cursor.getString(columnIndex); } } 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()); } }
相关推荐
Uri转Path
Android中Uri和Path之间的转换 原因 调用系统拍照应用,拍照后要保存图片,那么我们需要指定一个存储图片路径的Uri。这就涉及到如何将file path转换为Uri。有时候我们还需要根据照片的路径得到照片的media Uri,那么...
尿路 将相对文件系统路径转换为安全URI路径安装npm install --save uri-path用法var URIpath = require ( 'uri-path' ) ;// Properly encode URI path segmentsURIpath ( '../abc/@#$%¨&()[]{}-_=+ß/môòñ 月 ق...
uri转字符串路径,解决4.0以上的兼容问题,
下面小编就为大家带来一篇android图片文件的路径地址与Uri的相互转换方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
2、讲一个Uri转换成一个path 以选择一张图片为例: String path = FileTools.getRealPathFromUri(content,uri); //自定义方法在下面 public static String getRealPathFromUri(Context context,
URI,URL常识,需要的可以看看
从文件uri获取真实文件路径 安装(iOS) 目前不支持 安装(Android) npm i react-native-get-real-path@https://github.com/Wraptime/react-native-get-real-path.git --save 对以下文件进行更改: android/...
一些工具类代码块的标准代码,包括但不限于: 根据drawable id获取Bitmap 根据drawable id获取Drawable bitmap转drawable ...图片uri转path 根据路径获得突破并压缩返回bitmap用于显示 从文件中获取图片
通过URI获取文件Path(支持QQ文件管理器,下载文件夹,共享媒体文件)
可寻址的 主页 作者 版权 ...uri = Addressable :: URI . parse ( "http://example.com/path/to/resource/" ) uri . scheme #=> "http" uri . host #=> "example.com" uri . path #=> "/path/to/resou
eql ( u8 , uri.path, "/documentation/master/" )); assert (mem. eql ( u8 , uri.fragment, "toc-Introduction" )); Zig版本 默认情况下,master分支版本需要最新的Zig master版本。 对于Zig 0.6.0,使用提交89...
在项目中需要用到将Uri转换为绝对路径,在网上找到一个方法,做个笔记 网上有不少方法,但是有的对4.4后的版本无效,这里的方法可以在4.4之后的版本将Uri转换为绝对路径 public class GetPathFromUri { /** * 专...
它支持将字符串解析为scheme , authority , path , query和fragment URI组件,如下面所定义: : foo://example.com:8042/over/there?name=ferret#nose \_/ \______________/\_________/ \_________/ \__/ | ...
$ uri = new Uri ( 'http://user:pass@localhost:3000/path?query#fragment' ); // creates a new URI with a factory (is equivalent to `new Uri(...)`) $ uri = ( new UriFactory )-> createUri ( '...
path-normalize处理uri编码的字符串。该库的大部分直接来自。安装npm i path-normalize 用法const normalize = require ( 'path-normalize' )const normal = ( somePath ) => normalize ( somePath ) 现实世界中的...
URI.js URI.js是适用于所有JavaScript环境(浏览器,Node.js等)的兼容,可扩展方案的URI解析/...//returns://{// scheme : "uri",// userinfo : "user:pass",// host : "example.com",// port : 123,// path : "/one/
今天实现一个涂鸦效果,会分几步实现,这里有一个重要的知识点就是图层,要理解这个,不然你看这篇博客,很迷茫,...import android.graphics.Path; import android.util.AttributeSet; import android.view.MotionEvent; i
int nu_parse_uri(const char* _buf, size_t len, const char** scheme, size_t *scheme_len, const char **host, size_t *host_len, int *port, const char **path_query, int*path_query_len); 将 URI 字符串解析...