一、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. Python安装与基本数据类型

    人生苦短,我选Python. Python比其他的语言来说真的简洁多了,很多时候想做的东西都有对应的模块可以导入,平时玩点小东西真心不错. 首先讲一下安装,其实没什么好讲的,点点点点点,完事. 这里的 ...

  2. 【.Net 学习系列】-- .Net 指定时间段内定时执行的Windows服务(System.Threading.Thread)

    创建一个Windows服务项目:解决方案(右击)——> 添加 ——> 新建项目——>项目类型选择Windows——>模板选择Windows服务 ,如图: 编写Windows服务 ...

  3. ubuntu12.04+cuda6.0+opencv2.4.9

    更新了cuda之后,opencv的gpu模块又要重新编译了,这个地方有一个疑问,我对cuda6.0装了两次,第一次装好之后,没有配一个bumblebee,重装了cuda6.0之后,发现原来编译的ope ...

  4. 将github上的项目源码导入eclipse详细教程

    将github上的项目源码导入eclipse详细教程 学习了: http://blog.csdn.net/itbiggod/article/details/78462720

  5. HDU 1030 数学题

    给出两点,求这两点在图上的最短路径 分别以最上,左下,右下为顶点,看这个三角图形 ans=这三种情况下两点的层数差 #include "stdio.h" #include &quo ...

  6. ios学习8_KVC和字典转模型

    Key Value Coding是cocoa的一个标准组成部分,它能让我们能够通过name(key)的方式訪问属性,某些情况下极大地简化了代码.可称之为cocoa的大招. 例如以下的样例: 使用KVC ...

  7. HDU 4950 Monster(公式)

    HDU 4950 Monster 题目链接 题意:给定怪兽血量h,你攻击力a.怪物回血力b,你攻击k次要歇息一次,问是否能杀死怪兽 思路:签到题,注意最后一下假设打死了怪,那么怪就不会回血了 思路: ...

  8. centos部署jenkins

    1. 实验环境:   操作系统: CentOS Linux release 7.2.1511 (Core) 软件版本: jdk-8u60-linux-x64    apache-tomcat-9.0. ...

  9. YTU 2530: 小勇玩lol

    2530: 小勇玩lol 时间限制: 1 Sec  内存限制: 128 MB 提交: 112  解决: 77 题目描述 小勇是一个忠实的lol玩家,他有自己的战斗力计算方法,每个星期他都会算一下自己的 ...

  10. luogu 1901 发射站

    题目大意: 一个数列,它左边第一个比它高的人和右边第一个比它高的人要加上它的权值 思路: 单调栈维护一个单调递减的栈 正反各维护一遍 #include<iostream> #include ...