一、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. COGS2479(四维偏序)

    题意:给定一个有n个元素的序列,元素编号为1~n,每个元素有三个属性a,b,c,求序列中满足i<j且ai<aj且bi<bj且ci<cj的数对(i,j)的个数. 分析:cdq分治 ...

  2. Ubuntu 16.04安装7zip的图形界面工具PeaZip

    其实PeaZip不是7zip的图形界面工具,而是一整套方案,里面包括了7z格式的解压缩等. PeaZip Linux版本只有32位包,如果你使用的是64位Ubuntu系统,那么先打开终端运行下面的命令 ...

  3. Ubuntu 16.04 GNOME在桌面左侧添加启动器(Launcher)

    安装Dash to Dock: git clone https://github.com/micheleg/dash-to-dock.git cd dash-to-dock/ make make in ...

  4. 免费好用的Microsoft iSCSI Software Target 3.3

    我们在搭建Windows群集的时候往往会使用到IP-SAN.但是面对昂贵的硬件IP-SAN,我们在学习和实验的环境中更加多的用到的往往是软件模拟的IP-SAN.今天就给大家介绍一个微软出品的免费iSC ...

  5. 关于camera senor的power引脚问题

    <CamDevie> <HardWareInfo> <Sensor> <SensorName name="OV5640" >< ...

  6. kafka 生产者消费者 api接口

    生产者 import java.util.Properties; import kafka.javaapi.producer.Producer; import kafka.producer.Keyed ...

  7. 2016/1/12 String 笔记整理

    String  简介                        文件名 Teststring 有实例 String类 即字符串类型,并不是Java的基本数据类型,但可以像基本数据类型一样使用,用双 ...

  8. onload onmouseover 事件监听

    <div class="nav"> <ul> <li>翠翠</li> <li>嗯嗯</li> <li& ...

  9. swoole简易实时聊天

    最近公司拓展业务,需要做一个即时聊天业务,就顺带研究了一下swoole,文档地址贴出来:https://wiki.swoole.com/ 文档写得很详细,demo也很简洁明了,所以废话就不多说了. 工 ...

  10. [Codeforces 339D] Xenia and Bit Operations

    [题目链接] https://codeforces.com/problemset/problem/339/D [算法] 线段树模拟即可 时间复杂度 :O(MN) [代码] #include<bi ...