一、Android4.4版本以上Uri地址封装规范:

content://com.android.providers.media.documents/document/image%3A659

二、Android4.4版本以下Uri地址没被封装的,地址规范:

/storage/emulated/0/Pictures/07 绚彩夜拍.jpg

三、工具类代码实现:

  1. public class GetPathFromUri4kitkat {
  2. @SuppressLint("NewApi")
  3. public static String getPath(final Context context, final Uri uri) {
  4. final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
  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. if ("primary".equalsIgnoreCase(type)) {
  13. return Environment.getExternalStorageDirectory() + "/" + split[1];
  14. }
  15. // TODO handle non-primary volumes
  16. }
  17. // DownloadsProvider
  18. else if (isDownloadsDocument(uri)) {
  19. final String id = DocumentsContract.getDocumentId(uri);
  20. final Uri contentUri = ContentUris.withAppendedId(
  21. Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
  22. return getDataColumn(context, contentUri, null, null);
  23. }
  24. // MediaProvider
  25. else if (isMediaDocument(uri)) {
  26. final String docId = DocumentsContract.getDocumentId(uri);
  27. final String[] split = docId.split(":");
  28. final String type = split[0];
  29. Uri contentUri = null;
  30. if ("image".equals(type)) {
  31. contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
  32. } else if ("video".equals(type)) {
  33. contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
  34. } else if ("audio".equals(type)) {
  35. contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
  36. }
  37. final String selection = "_id=?";
  38. final String[] selectionArgs = new String[] { split[1] };
  39. return getDataColumn(context, contentUri, selection, selectionArgs);
  40. }
  41. }
  42. // MediaStore (and general)
  43. else if ("content".equalsIgnoreCase(uri.getScheme())) {
  44. return getDataColumn(context, uri, null, null);
  45. }
  46. // File
  47. else if ("file".equalsIgnoreCase(uri.getScheme())) {
  48. return uri.getPath();
  49. }
  50. return null;
  51. }
  52. /**
  53. * Get the value of the data column for this Uri. This is useful for
  54. * MediaStore Uris, and other file-based ContentProviders.
  55. *
  56. * @param context
  57. *            The context.
  58. * @param uri
  59. *            The Uri to query.
  60. * @param selection
  61. *            (Optional) Filter used in the query.
  62. * @param selectionArgs
  63. *            (Optional) Selection arguments used in the query.
  64. * @return The value of the _data column, which is typically a file path.
  65. */
  66. public static String getDataColumn(Context context, Uri uri, String selection,
  67. String[] selectionArgs) {
  68. Cursor cursor = null;
  69. final String column = "_data";
  70. final String[] projection = { column };
  71. try {
  72. cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
  73. null);
  74. if (cursor != null && cursor.moveToFirst()) {
  75. final int column_index = cursor.getColumnIndexOrThrow(column);
  76. return cursor.getString(column_index);
  77. }
  78. } finally {
  79. if (cursor != null)
  80. cursor.close();
  81. }
  82. return null;
  83. }
  84. /**
  85. * @param uri
  86. *            The Uri to check.
  87. * @return Whether the Uri authority is ExternalStorageProvider.
  88. */
  89. public static boolean isExternalStorageDocument(Uri uri) {
  90. return "com.android.externalstorage.documents".equals(uri.getAuthority());
  91. }
  92. /**
  93. * @param uri
  94. *            The Uri to check.
  95. * @return Whether the Uri authority is DownloadsProvider.
  96. */
  97. public static boolean isDownloadsDocument(Uri uri) {
  98. return "com.android.providers.downloads.documents".equals(uri.getAuthority());
  99. }
  100. /**
  101. * @param uri
  102. *            The Uri to check.
  103. * @return Whether the Uri authority is MediaProvider.
  104. */
  105. public static boolean isMediaDocument(Uri uri) {
  106. return "com.android.providers.media.documents".equals(uri.getAuthority());
  107. }
  108. }

Android4.4以上Uri转换成绝对路径的工具类的更多相关文章

  1. Java中windows路径转换成linux路径等工具类

    项目中发现别人写好的操作系统相关的工具类: 我总结的类似相关博客:http://www.cnblogs.com/DreamDrive/p/4289860.html import java.net.In ...

  2. 数据库表转换成javaBean对象小工具

    package test.utils; import java.io.FileWriter;import java.io.IOException;import java.io.PrintWriter; ...

  3. 为什么要用Hibernate框架? 把SessionFactory,Session,Transcational封装成包含crud的工具类并且处理了事务,那不是用不着spring了?

    既然用Hibernate框架访问管理持久层,那为何又提到用Spring来管理以及整合Hibernate呢?把SessionFactory,Session,Transcational封装成包含crud的 ...

  4. 【游戏开发】Excel表格批量转换成CSV的小工具

    一.前言 在工作的过程中,我们有时可能会面临将Excel表格转换成CSV格式文件的需求.这尤其在游戏开发中体现的最为明显,策划的数据文档大多是一些Excel表格,且不说这些表格在游戏中读取的速度,但就 ...

  5. illustrator将图片转换成ai路径

    窗口->图像描摹 第一选择模式:彩色 第二步:调整颜色的数值 最后点击工具栏上的扩展按钮转成路径 搞定~

  6. px转换成bp单位的工具函数

    import {Dimensions} from 'react-native' //当前屏幕的高度 const deviceH = Dimensions.get('window').height // ...

  7. 调用jdbc已经写成的方法----jdbc工具类抽取方式三

    package jdbc_demo3; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.R ...

  8. 调用jdbc已经写成的方法----jdbc工具类抽取方式二

    先创建db.properties driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/web08?useUnicode=true& ...

  9. 调用jdbc已经写成的方法----jdbc工具类抽取方式一

    package web09; /*获取连接和释放资源的方法 */ import java.sql.Connection; import java.sql.DriverManager; import j ...

随机推荐

  1. HDU——1150 Machine Schedule

    Machine Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  2. java web 验证码 第一次不正确的问题,解决方案

    首先是form表单 ,获取图片验证码 然后使用js 去服务器验证 问题: 第一次明明输入正确 ,确验证不了??那是因为你在form表单发起请求 和 ajax  发起的请求  地址 中 一个使用127. ...

  3. Redis持久化方式--RDB和AOF

    转载于:https://www.cnblogs.com/xingzc/p/5988080.html Redis提供了RDB持久化和AOF持久化 RDB机制的优势和略施 RDB持久化是指在指定的时间间隔 ...

  4. 选择器的使用(target选择器)

    <!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head><meta ...

  5. Linux下使用make install安装的软件如何卸载

    如果是Ubuntu的系统,那么可以使用checkinstall来生成deb包来安装,然后卸载 参考:http://blog.sina.com.cn/s/blog_4178f4bf0101cmt7.ht ...

  6. Microsoft Office 2016 for win10 全版本下载+注册激活_Office教程学习网

    Microsoft Office 2016 for win10 全版本下载+注册激活_Office教程学习网 http://pan.baidu.com/s/1qWxdvT6

  7. RIP

    距离矢量路由协议 假设网络拓扑如下 192.168.1.0网段 - - - - R1 - - 192.168.12.0网段 - - R2 - - 192.168.23.0网段 - - R3 - - - ...

  8. 智能眼镜技术科普:VR、AR、MR的区别

    前段时间, 获得谷歌5亿美元融资的技术公司Magic Leap在WSJD展会中放出了一段实录视频,引起不小骚动.如今,也有媒体称他们为MR公司,那么VR.AR.MR之间到底有什么区别呢. VR.AR. ...

  9. 牛腩新闻系统(一)——UML、数据库设计

    牛腩新闻系统(一)--UML.数据库设计 一.初识牛腩系统 牛腩(Brisket)即牛腹部及靠近牛肋处的松软肌肉,是指带有筋.肉.油花的肉 块.这是一种统称. 若依部位来分,牛身上很多地方的肉都能够叫 ...

  10. [欧拉回路] poj 1300 Door Man

    题目链接: http://poj.org/problem?id=1300 Door Man Time Limit: 1000MS   Memory Limit: 10000K Total Submis ...