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

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. Oracle总结之plsql编程(基础九)

    原创作品,转自请注明出处:https://www.cnblogs.com/sunshine5683/p/10344302.html 接着上次总结,继续今天的总结,今天主要总结plsql中控制语句,如条 ...

  2. class文件打包成jar

    电脑左下角“开始”——“运行”——输入cmd——cd+空格+clss文件所在文件夹的路径——jar+空格+-cf+空格+“jar包的名字”.jar+空格+*.class.好了...

  3. C#连接Oracle方式

    1.在web.config或者app.config文件中,不会像连接SqlServer的格式那样,直接把账号密码写入就可以了 C#是微软的,oracle是Oracle公司做出来的,两家是对立的,都懂得 ...

  4. Codeforces675D(SummerTrainingDay06-J)

    D. Tree Construction time limit per test:2 seconds memory limit per test:256 megabytes input:standar ...

  5. js-权威指南学习笔记19

    第十九章 jQuery类库 1.传递HTML文本字符串给$()方法,jQuery会根据传入的文本创建好HTML元素并封装为jQuery对象返回. 2.想要遍历jQuery对象中的所有元素时,可以调用e ...

  6. js 动态声明变量

    var  object = {}; for(var i=0; i<5; i++){ object['attr'+i] = i; }

  7. Struts 2(二)

    一,框架的扩展名问题: ,struts2框架的默认扩展名:.action和空字符串.在框架的属性文件default.properties中进行默认配置:struts.action.extension= ...

  8. hadoop重启后,hdfs目录权限问题

    今天重启了下Hadoop集群,使用Eclipse调试hdfs api的时候报错: [WARNING] java.lang.NullPointerException at org.conan.kafka ...

  9. Linux 配置iso系统盘为本地yum源

    Linux配置iso系统盘为本地yum源 by:授客 QQ:1033553122   1.目的 安装软件时,经常会遇到包或类库的依赖性问题,为此,我们可以通过yum命令安装软件,尽量避免出现繁琐的软件 ...

  10. PHP多进程编程之僵尸进程问题

    上一篇说到了使用pcntl_fork函数可以让PHP实现多进程并发或者异步处理的效果.那么问题是我们产生的进程需要去控制,而不能置之不理.最基本的方式就是fork进程和杀死进程. 通过利用pcntl_ ...