简易版处理(实际并没发现有什么问题)

public static String getRealPathFromURI(Context context, Uri contentURI) {
String result;
Cursor cursor = context.getContentResolver().query(contentURI,
new String[]{MediaStore.Images.ImageColumns.DATA},//
null, null, null);
if (cursor == null) result = contentURI.getPath();
else {
cursor.moveToFirst();
int index = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
result = cursor.getString(index);
cursor.close();
}
return result;
}
 

复杂版处理

/**
* 根据图片的Uri获取图片的绝对路径(适配多种API)
*
* @return 如果Uri对应的图片存在, 那么返回该图片的绝对路径, 否则返回null
*/
public static String getRealPathFromUri(Context context, Uri uri) {
int sdkVersion = Build.VERSION.SDK_INT;
if (sdkVersion < 11) return getRealPathFromUri_BelowApi11(context, uri);
if (sdkVersion < 19) return getRealPathFromUri_Api11To18(context, uri);
else return getRealPathFromUri_AboveApi19(context, uri);
} /**
* 适配api19以上,根据uri获取图片的绝对路径
*/
@TargetApi(Build.VERSION_CODES.KITKAT)
private static String getRealPathFromUri_AboveApi19(Context context, Uri uri) {
String filePath = null;
String wholeID = DocumentsContract.getDocumentId(uri); // 使用':'分割
String id = wholeID.split(":")[1]; String[] projection = {MediaStore.Images.Media.DATA};
String selection = MediaStore.Images.Media._ID + "=?";
String[] selectionArgs = {id}; Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,//
projection, selection, selectionArgs, null);
int columnIndex = cursor.getColumnIndex(projection[0]);
if (cursor.moveToFirst()) filePath = cursor.getString(columnIndex);
cursor.close();
return filePath;
} /**
* 适配api11-api18,根据uri获取图片的绝对路径
*/
private static String getRealPathFromUri_Api11To18(Context context, Uri uri) {
String filePath = null;
String[] projection = {MediaStore.Images.Media.DATA};
CursorLoader loader = new CursorLoader(context, uri, projection, null, null, null);
Cursor cursor = loader.loadInBackground(); if (cursor != null) {
cursor.moveToFirst();
filePath = cursor.getString(cursor.getColumnIndex(projection[0]));
cursor.close();
}
return filePath;
} /**
* 适配api11以下(不包括api11),根据uri获取图片的绝对路径
*/
private static String getRealPathFromUri_BelowApi11(Context context, Uri uri) {
String filePath = null;
String[] projection = {MediaStore.Images.Media.DATA};
Cursor cursor = context.getContentResolver().query(uri, projection, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
filePath = cursor.getString(cursor.getColumnIndex(projection[0]));
cursor.close();
}
return filePath;
}
 

根据Uri获取文件的绝对路径的更多相关文章

  1. iOS 获取文件的目录路径的几种方法 [转]

    iOS 获取文件的目录路径的几种方法 2 years ago davidzhang iphone沙箱模型的有四个文件夹,分别是什么,永久数据存储一般放在什么位置,得到模拟器的路径的简单方式是什么. d ...

  2. JSTL获取Session的ID与获取文件的真实路径与项目名称

    今天在测试集群配置的时候想到session共享,因此想要获取sessionID,可以通过下面方法: ${pageContext.session.id} 获取文件的真实路径: <%=request ...

  3. fileReader对象读取txt文件乱码问题 以及如何获取文件的url路径(绝对路径)

    <input type="file" @change="aaa($event)"> <div id="hi">< ...

  4. python实现获取文件的绝对路径

    实现代码如下: #获取文件的绝对路径import osclass GetPath: def get_path(self,path): r=os.path.abspath(path) return ri ...

  5. Android根据图片Uri获取图片path绝对路径的几种方法【转】

    在Android 编程中经常会用到Uri转化为文件路径,如我们从相册选择图片上传至服务器,一般上传前需要对图片进行压缩,这时候就要用到图片的绝对路径. 下面对我开发中uri转path路径遇到的问题进行 ...

  6. 定时发布任务,在global.asax中获取文件的物理路径的方法

    如果要把一个相对路径或者虚拟路径映射道服务器的物理路径,通常会使用Server.MapPath()函数,比如将根目录下的html目录映射为物理路径:Server.MapPath("html& ...

  7. PHP获取文件的绝对路径

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 ===========PH ...

  8. C#获取文件的绝对路径

    要在c#中获取路径有好多方法,一般常用的有以下五种: //获取应用程序的当前工作目录. String path1 = System.IO.Directory.GetCurrentDirectory() ...

  9. php中如何获取文件的正确路径

    以上面的图片为例子 //我们这里需要使用到 $_FILE echo "<pre>"; print_r($_FILES); echo $_SERVER['DOCUMENT ...

随机推荐

  1. VMware设置桥接上网

    转自:http://blog.csdn.net/gavin_dinggengjia/article/details/6325904 环境:主机Win7.VMware Workstation 6.5.3 ...

  2. Java - "JUC" ReentrantLock获取锁

    [Java并发编程实战]-----“J.U.C”:ReentrantLock之一简介 ReentrantLock介绍 ReentrantLock是一个可重入的互斥锁,又被称为“独占锁”. 顾名思义,R ...

  3. hadoop配置历史服务器&&配置日志聚集

    配置历史服务器 1.在mapred-site.xml中写入一下配置 <property> <name>mapreduce.jobhistory.address</name ...

  4. [HNOI2011]括号修复

    设\(nd[4]\) 0--多出来的右括号 1--多出来的左括号 2--取反后多出来的右括号 3--取反后多出来的左括号 这样一来 Swap: swap(0,3),swap(1,2),swap(sn[ ...

  5. es6 类 和构造函数

  6. Android ConstraintLayout详解(from jianshu)

    Android ConstraintLayout详解 https://www.jianshu.com/p/a8b49ff64cd3 1. 概述     在本篇文章中,你会学习到有关Constraint ...

  7. KeyPress 和KeyDown 、KeUp之间的区别

    前几天,在写完一个功能模块上线测试的时候,出现了一个诡异的问题.input 框在输入查询内容之后,按回车键居然有两种不同的表现形式(input 框没有绑定键盘事件),谷歌和火狐功能正常,但IE在按了回 ...

  8. Django组件扩展 总结

    1. Form组件扩展: 验证用户输入 obj = Form(reuest,POST,request.FILES) if obj.is_valid(): obj.clean_data else: ob ...

  9. D3、EChart、HighChart绘图demol

    1.echarts:   <!DOCTYPE html>   <html>   <head>   <meta charset="utf-8" ...

  10. Git删除文件

    Git基础 Git有三大区(工作区.暂存区.版本库),文件有三个状态(untracked.unstaged.uncommited). (1)打开项目文件夹,除了隐藏的.git文件夹,其他项目文件位于的 ...