Android4.4以上Uri转换成绝对路径的工具类
一、Android4.4版本以上Uri地址封装规范:
content://com.android.providers.media.documents/document/image%3A659
二、Android4.4版本以下Uri地址没被封装的,地址规范:
/storage/emulated/0/Pictures/07 绚彩夜拍.jpg
三、工具类代码实现:
- public class GetPathFromUri4kitkat {
- @SuppressLint("NewApi")
- 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());
- }
- }
Android4.4以上Uri转换成绝对路径的工具类的更多相关文章
- Java中windows路径转换成linux路径等工具类
项目中发现别人写好的操作系统相关的工具类: 我总结的类似相关博客:http://www.cnblogs.com/DreamDrive/p/4289860.html import java.net.In ...
- 数据库表转换成javaBean对象小工具
package test.utils; import java.io.FileWriter;import java.io.IOException;import java.io.PrintWriter; ...
- 为什么要用Hibernate框架? 把SessionFactory,Session,Transcational封装成包含crud的工具类并且处理了事务,那不是用不着spring了?
既然用Hibernate框架访问管理持久层,那为何又提到用Spring来管理以及整合Hibernate呢?把SessionFactory,Session,Transcational封装成包含crud的 ...
- 【游戏开发】Excel表格批量转换成CSV的小工具
一.前言 在工作的过程中,我们有时可能会面临将Excel表格转换成CSV格式文件的需求.这尤其在游戏开发中体现的最为明显,策划的数据文档大多是一些Excel表格,且不说这些表格在游戏中读取的速度,但就 ...
- illustrator将图片转换成ai路径
窗口->图像描摹 第一选择模式:彩色 第二步:调整颜色的数值 最后点击工具栏上的扩展按钮转成路径 搞定~
- px转换成bp单位的工具函数
import {Dimensions} from 'react-native' //当前屏幕的高度 const deviceH = Dimensions.get('window').height // ...
- 调用jdbc已经写成的方法----jdbc工具类抽取方式三
package jdbc_demo3; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.R ...
- 调用jdbc已经写成的方法----jdbc工具类抽取方式二
先创建db.properties driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/web08?useUnicode=true& ...
- 调用jdbc已经写成的方法----jdbc工具类抽取方式一
package web09; /*获取连接和释放资源的方法 */ import java.sql.Connection; import java.sql.DriverManager; import j ...
随机推荐
- Python安装与基本数据类型
人生苦短,我选Python. Python比其他的语言来说真的简洁多了,很多时候想做的东西都有对应的模块可以导入,平时玩点小东西真心不错. 首先讲一下安装,其实没什么好讲的,点点点点点,完事. 这里的 ...
- 【.Net 学习系列】-- .Net 指定时间段内定时执行的Windows服务(System.Threading.Thread)
创建一个Windows服务项目:解决方案(右击)——> 添加 ——> 新建项目——>项目类型选择Windows——>模板选择Windows服务 ,如图: 编写Windows服务 ...
- ubuntu12.04+cuda6.0+opencv2.4.9
更新了cuda之后,opencv的gpu模块又要重新编译了,这个地方有一个疑问,我对cuda6.0装了两次,第一次装好之后,没有配一个bumblebee,重装了cuda6.0之后,发现原来编译的ope ...
- 将github上的项目源码导入eclipse详细教程
将github上的项目源码导入eclipse详细教程 学习了: http://blog.csdn.net/itbiggod/article/details/78462720
- HDU 1030 数学题
给出两点,求这两点在图上的最短路径 分别以最上,左下,右下为顶点,看这个三角图形 ans=这三种情况下两点的层数差 #include "stdio.h" #include &quo ...
- ios学习8_KVC和字典转模型
Key Value Coding是cocoa的一个标准组成部分,它能让我们能够通过name(key)的方式訪问属性,某些情况下极大地简化了代码.可称之为cocoa的大招. 例如以下的样例: 使用KVC ...
- HDU 4950 Monster(公式)
HDU 4950 Monster 题目链接 题意:给定怪兽血量h,你攻击力a.怪物回血力b,你攻击k次要歇息一次,问是否能杀死怪兽 思路:签到题,注意最后一下假设打死了怪,那么怪就不会回血了 思路: ...
- centos部署jenkins
1. 实验环境: 操作系统: CentOS Linux release 7.2.1511 (Core) 软件版本: jdk-8u60-linux-x64 apache-tomcat-9.0. ...
- YTU 2530: 小勇玩lol
2530: 小勇玩lol 时间限制: 1 Sec 内存限制: 128 MB 提交: 112 解决: 77 题目描述 小勇是一个忠实的lol玩家,他有自己的战斗力计算方法,每个星期他都会算一下自己的 ...
- luogu 1901 发射站
题目大意: 一个数列,它左边第一个比它高的人和右边第一个比它高的人要加上它的权值 思路: 单调栈维护一个单调递减的栈 正反各维护一遍 #include<iostream> #include ...