android图片文件的路径地址与Uri的相互转换方法
作者:jingxian 时间:2023-09-09 20:11:45
一个android文件的Uri地址一般如下:
content://media/external/images/media/62026
这是一张图片的Uri,那么我们如何根据这个Uri获得其在文件系统中的路径呢?
其实很简单,直接上代码:
public static String getRealFilePath( final Context context, final Uri uri ) {
if ( null == uri ) return null;
final String scheme = uri.getScheme();
String data = null;
if ( scheme == null )
data = uri.getPath();
else if ( ContentResolver.SCHEME_FILE.equals( scheme ) ) {
data = uri.getPath();
} else if ( ContentResolver.SCHEME_CONTENT.equals( scheme ) ) {
Cursor cursor = context.getContentResolver().query( uri, new String[] { ImageColumns.DATA }, null, null, null );
if ( null != cursor ) {
if ( cursor.moveToFirst() ) {
int index = cursor.getColumnIndex( ImageColumns.DATA );
if ( index > -1 ) {
data = cursor.getString( index );
}
}
cursor.close();
}
}
return data
}
如我们有一个图片的路径地址又该如何获得其Uri呢?
String type = Utils.ensureNotNull(intent.getType());
Log.d(TAG, "uri is " + uri);
if (uri.getScheme().equals("file") && (type.contains("image/"))) {
String path = uri.getEncodedPath();
Log.d(TAG, "path1 is " + path);
if (path != null) {
path = Uri.decode(path);
Log.d(TAG, "path2 is " + path);
ContentResolver cr = this.getContentResolver();
StringBuffer buff = new StringBuffer();
buff.append("(")
.append(Images.ImageColumns.DATA)
.append("=")
.append("'" + path + "'")
.append(")");
Cursor cur = cr.query(
Images.Media.EXTERNAL_CONTENT_URI,
new String[] { Images.ImageColumns._ID },
buff.toString(), null, null);
int index = 0;
for (cur.moveToFirst(); !cur.isAfterLast(); cur
.moveToNext()) {
index = cur.getColumnIndex(Images.ImageColumns._ID);
// set _id value
index = cur.getInt(index);
}
if (index == 0) {
//do nothing
} else {
Uri uri_temp = Uri
.parse("content://media/external/images/media/"
+ index);
Log.d(TAG, "uri_temp is " + uri_temp);
if (uri_temp != null) {
uri = uri_temp;
}
}
}
}
标签:android,uri,文件路径
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Spring Cloud Gateway去掉url前缀
2023-06-05 00:54:38
Java实现添加文字水印&图片水印的方法详解
2023-01-28 08:11:39
![](https://img.aspxhome.com/file/2023/5/110795_0s.png)
Android嵌套滚动NestedScroll的实现了解一下
2022-09-10 13:48:57
![](https://img.aspxhome.com/file/2023/5/101825_0s.gif)
Unity实现聊天室功能
2023-12-20 19:53:34
![](https://img.aspxhome.com/file/2023/2/119542_0s.jpg)
SpringBoot如何整合redis实现过期key监听事件
2023-08-04 18:51:19
![](https://img.aspxhome.com/file/2023/8/57808_0s.png)
vscode设置Fira_Code字体及改变编辑器字体、背景颜色的代码详解
2023-05-22 21:00:17
![](https://img.aspxhome.com/file/2023/9/114679_0s.jpg)
Qt GUI图形图像开发之Qt表格控件QTableView简单使用方法及QTableView与QTableWidget区别
2022-02-23 05:02:59
![](https://img.aspxhome.com/file/2023/4/117774_0s.jpg)
SpringBoot整合log4j2日志的实现
2021-11-14 23:41:01
![](https://img.aspxhome.com/file/2023/5/103805_0s.png)
SpringBoot如何使用RateLimiter通过AOP方式进行限流
2023-09-16 18:18:44
![](https://img.aspxhome.com/file/2023/3/58583_0s.png)
SpringBoot文件上传大小设置方式(yml中配置)
2021-09-21 09:26:47
Springboot实现多数据源切换详情
2023-06-12 12:02:43
Java集合教程之Collection实例详解
2022-12-01 23:45:13
![](https://img.aspxhome.com/file/2023/4/104704_0s.jpg)
Unity实现滑动更换界面效果
2021-10-06 19:56:40
![](https://img.aspxhome.com/file/2023/6/84466_0s.jpg)
Java基础之String类使用与字符串比较
2023-11-09 23:46:51
Java实现经典游戏飞机大战-I的示例代码
2023-07-30 15:45:22
![](https://img.aspxhome.com/file/2023/1/68751_0s.jpg)
C#中的委托和事件详解
2022-01-20 01:31:53
![](https://img.aspxhome.com/file/2023/2/113702_0s.png)
Spring Cloud动态配置刷新RefreshScope使用示例详解
2022-05-23 15:05:32
Android 四种动画效果的调用实现代码
2021-06-26 17:59:54
![](https://img.aspxhome.com/file/2023/4/126764_0s.gif)
Spring Boot右键maven build成功但是直接运行main方法出错的解决方案
2021-08-22 00:21:26
![](https://img.aspxhome.com/file/2023/1/129121_0s.png)
C#实现Base64编码与解码及规则
2023-09-17 13:06:19