一、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. openOffice word转pdf,pdf转图片优化版

    之前写了一个版本的,不过代码繁琐而且不好用,效率有些问题.尤其pdf转图片速度太慢.下面是优化版本的代码. spriing_boot 版本信息:2.0.1.RELEASE 1.配置信息: packag ...

  2. Django学习系列之captcha 验证码插件

    安装部署 安装captcha pip3. install django-simple-captcha== settings.py中引入captcha INSTALLED_APPS = [ 'djang ...

  3. {head first} --- networking 1

    Head first系列的书确实非常好,深入浅出解说网络的组成.让曾经那些生涩的概念生动起来. Chapter 1 维修物理网络 CAT5电缆: 两端为RJ-45接头(水晶头).内部为UTP(非屏蔽双 ...

  4. HDU 5265 pog loves szh II (二分查找)

    [题目链接]click here~~ [题目大意]在给定 的数组里选两个数取模p的情况下和最大 [解题思路]: 思路见官方题解吧~~ 弱弱献上代码: Problem : 5265 ( pog love ...

  5. Velocity高速新手教程

    变量 (1)变量的定义: #set($name = "hello")      说明:velocity中变量是弱类型的. 当使用#set 指令时,括在双引號中的字面字符串将解析和又 ...

  6. Unity3D & C# 设计模式--23

     Unity3D & C#Design Patterns 23 design patterns. Creational Patterns 1. Abstract Factory抽象工厂 创 ...

  7. (二)Java 简介

    Java 简介 Java是由Sun Microsystems公司于1995年5月推出的Java面向对象程序设计语言和Java平台的总称.由James Gosling和同事们共同研发,并在1995年正式 ...

  8. Codeforces Round #323 (Div. 2) D. Once Again... 暴力+最长非递减子序列

                                                                                  D. Once Again... You a ...

  9. 修改数据表的字符集为utf8mb4

    修改数据表的字符集为utf8mb4

  10. 20170623_oracle_优化与体系结构

    一般优化技巧 建议不用"*"代替所有列名 删除所有数据用TRUNCATE代替DELETE 用NOT EXISTS 代替NOT IN 用EXISTS代替IN 用EXISTS代替DIS ...