日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

从URI中获取实际的文件path

發布時間:2025/3/19 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 从URI中获取实际的文件path 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

如題,經常用在onActivityResult方法中解析圖片等各種地址,因為Android 4.4之后google更改了對應的方法。
/**
* 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) {

final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;// DocumentProviderif (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {// ExternalStorageProviderif (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}// DownloadsProviderelse if (isDownloadsDocument(uri)) {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);}// MediaProviderelse if (isMediaDocument(uri)) {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);}}// MediaStore (and general)else if ("content".equalsIgnoreCase(uri.getScheme())) {return getDataColumn(context, uri, null, null);}// Fileelse if ("file".equalsIgnoreCase(uri.getScheme())) {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 column_index = cursor.getColumnIndexOrThrow(column);return cursor.getString(column_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()); }

其實有時候也不用那么麻煩,直接一個獲取文件路徑的方法就可以了,不用做這么多判斷。

轉載于:https://www.cnblogs.com/likeshu/p/5416832.html

總結

以上是生活随笔為你收集整理的从URI中获取实际的文件path的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 亚洲午夜精品一区二区三区他趣 | 精品国产乱码久久久久久浪潮 | 爱豆国产剧免费观看大全剧集 | 真实乱偷全部视频 | 欧美在线播放 | 美女少妇一区二区 | 免费无码又爽又黄又刺激网站 | 香蕉综合网 | 精品国产乱码久久久久久浪潮 | 朝鲜黄色片| 中国免费黄色片 | 97性视频| 一级特黄av | 一本久久精品一区二区 | 国产又色又爽无遮挡免费 | 亚洲熟妇国产熟妇肥婆 | 激情四虎| 五月天综合激情网 | 久一在线 | 快播91 | 中文在线字幕免费观看 | 福利av在线| 在线观看精品视频 | 热久久网站 | 色妞干网 | 性色国产成人久久久精品 | 精品人妻一区二区三区蜜桃视频 | 色哟哟网站 | 福利片第一页 | 艹男人的日日夜夜 | 久草福利资源在线观看 | 国产良妇出轨视频在线观看 | 亚洲a视频在线 | 人妻丰满熟妇岳av无码区hd | japanese中文字幕 | 激情六月天婷婷 | 欧美日韩国产亚洲一区 | 国产网红在线观看 | 亚洲精品传媒 | 欧美精品免费播放 | av中文字幕一区二区三区 | 日本久久伊人 | 女人舌吻男人茎视频 | 久久久夜色精品亚洲 | 国产精品一区二区三区免费视频 | 91美女高潮出水 | 91黄在线观看 | 国产探花精品一区二区 | 一本大道一区二区 | 999久久久免费精品国产 | 爱爱视频天天干 | 日韩在线电影一区二区 | 亚洲精品视频免费在线观看 | 99久久久无码国产精品免费 | 久久免费av | 4438国产精品一区二区 | 日本在线免费播放 | 白峰美羽在线播放 | 黑人精品一区二区三区不 | 舒淇裸体午夜理伦 | 超碰在线日韩 | 无码人妻一区二区三区在线 | 精品在线观看视频 | 熟女丰满老熟女熟妇 | 免费久久精品视频 | 久久久av网站 | 国产99久久久国产精品成人免费 | 免费黄色一区二区 | 乱老熟女一区二区三区 | 国语对白对话在线观看 | 一级小毛片 | 波多野结衣视频免费看 | 91免费播放 | 国产乱码久久久久 | 熟妇熟女乱妇乱女网站 | 女人叉开腿让男人桶 | 午夜久久精品 | 黄色香蕉网 | 亚洲国产精品狼友在线观看 | 国产精品久久久久三级 | 操网| 西野翔之公侵犯中文字幕 | 国产伦精品一区二区三区免费迷 | 激情草逼 | 中文字幕av久久爽一区 | 淫片在线| 成人黄色免费在线观看 | 毛片aa | 五月婷婷基地 | 无码人妻久久一区二区三区 | 欧美黑丝少妇 | 性色一区二区三区 | 欧美视频一区在线 | 精品久久久久久久免费人妻 | 国产精品香蕉在线 | 国产五月| 国产av不卡一区二区 | 性xxx18| 黄色免费网页 |