如题,经常用在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;

    // DocumentProvider
if (isKitKat && 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
}
// DownloadsProvider
else 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);
}
// MediaProvider
else 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);
}
// File
else 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());
}

其实有时候也不用那么麻烦,直接一个获取文件路径的方法就可以了,不用做这么多判断。

从URI中获取实际的文件path的更多相关文章

  1. 在Delphi中获取和修改文件的时间

    转载自 http://www.cnblogs.com/jieke/archive/2013/01/11/2855782.html 本文介绍了在Delphi中利用系统函数和Windows API函数调用 ...

  2. 【转】在cmd/bat脚本中获取当前脚本文件所在目录

    一.关于cd的/d参数 关于cd 的/d参数,在cmd中敲入cd /?可以看到/d参数的解释如下: 使用 /D 命令行开关,除了改变驱动器的当前目录之外,还可改变当前驱动器.这句话不太好理解,我做个试 ...

  3. 网页中获取网络mp3文件的时常

    <html> <audio id="audio" controls> <source src="http://cdn.kaishuhezi. ...

  4. Android中获取目标布局文件中的组件

    方法如下: LayoutInflater flater= LayoutInflater.from(getContext()); //R.layout.title处填写目标布局 final View v ...

  5. Qt在windows与Mac OS中获取执行程序版本号

    1 windows中获取执行文件exe的版本号   QString GetFileVertion(QString aFullName) { QString vRetVersion; string vF ...

  6. 在SpringBoot项目中怎样引入.yml文件中的设置

    SpringBoot中获取application.yml文件内容 原始方式pro.load()与 pro.getProperty()配合的方式 @Value注解方式 @ConfigurationPro ...

  7. 与众不同 windows phone (37) - 8.0 文件系统: StorageFolder, StorageFile, 通过 Uri 引用文件, 获取 SD 卡中的文件

    [源码下载] 与众不同 windows phone (37) - 8.0 文件系统: StorageFolder, StorageFile, 通过 Uri 引用文件, 获取 SD 卡中的文件 作者:w ...

  8. 在swt中获取jar包中的文件 uri is not hierarchical

    uri is not hierarchical 学习了:http://blog.csdn.net/zdsdiablo/article/details/1519719 在swt中获取jar包中的文件: ...

  9. android 中获取视频文件的缩略图(非原创)

    在android中获取视频文件的缩略图有三种方法: 1.从媒体库中查询 2. android 2.2以后使用ThumbnailUtils类获取 3.调用jni文件,实现MediaMetadataRet ...

随机推荐

  1. Android ImageView的属性android:scaleType

    ImageView的属性android:scaleType,即ImageView.setScaleType(ImageView.ScaleType) imageView.setScaleType(Im ...

  2. LinkedHashSet的实现原理

    1. LinkedHashSet概述 LinkedHashSet是具有可预知迭代顺序的Set接口的哈希表和链接列表实现.此实现与HashSet的不同之处在于,后者维护着一个运行于所有条目的双重链接列表 ...

  3. MVVM中轻松实现Command绑定(三)任意事件的Command

    WPF中不是所有的控件都有Command属性的,如果窗体我需要在ViewModel中处理Loaded事件命令,或者其他事件的命令时,很难都过绑定Command完成,必须要注册依赖属性或事件等,太麻烦了 ...

  4. [POJ3684]Physics Experiment

      Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1363   Accepted: 476   Special Judge ...

  5. Java笔记(二十八)……IO流下 IO包中其他常用类以及编码表问题

    PrintWriter打印流 Writer的子类,既可以接收字符流,也可以接收字节流,还可以接收文件名或者文件对象,非常方便 同时,还可以设置自动刷新以及保持原有格式写入各种文本类型的print方法 ...

  6. 如何使用Visual Studio 2013 创建Azure云应用

    创建 Azure 云服务 Azure 云服务包括执行应用程序所需操作的角色.当你将云服务发布到 Azure 时,每个角色将在云中的虚拟机上运行.有关如何开发 Azure 云服务的详细信息. 创建 Az ...

  7. uva_12535 - Probability Through Experiments

    题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  8. linux下查询域名或IP注册信息的操作记录(whois)

    在运维工作中,有时需要查询某些域名的注册信息(域名的NS,注册用户,注册邮箱等),可以使用whois这个命令.whois命令令用来查找并显示指定帐号(或域名)的用户相关信息,因为它是到Network ...

  9. Quartz 多个触发器

    http://www.oschina.net/code/snippet_114990_4440 最近项目中要做个定时生成静态html文件东东,7点到19点每5分钟生成一次,其他时间1小时生成一次,刚开 ...

  10. jQuery EasyUI, datagrid, treegrid formatter 参数比较 row index

    如题: datagrid中,见官方文档: formatter function The cell formatter function, take three parameter:value: the ...