Android4.4获取多媒体(图片,声音,视频)路径

本文介绍在Android4.4系统中从两种不同格式的Uri获取图片路径的方法。一种是通过DocumentsContract处理特有API返回的Uri,另一种是直接使用MediaStore查询普通Uri。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转自:http://www.2cto.com/kf/201312/264066.html

关于Android4.4的图片路径获取,如果回来的Uri的格式有两种

1
content: //com.android.providers.media.documents/document/image:3951
?
1
content: //media/external/images/media/3951

出现第一种Uri的操作方式如下图所示:

直接点选最近的图片,应该是Android4.4 特有的Api

. 加载中...

 

解决方案:

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
if (DocumentsContract.isDocumentUri(context, contentUri)){
     String wholeID = DocumentsContract.getDocumentId(contentUri);
     String id = wholeID.split(:)[ 1 ];
     String[] column = { MediaStore.Images.Media.DATA };
     String sel = MediaStore.Images.Media._ID + =?;
     Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, column,
             sel, new String[] { id }, null );
     int columnIndex = cursor.getColumnIndex(column[ 0 ]);
     if (cursor.moveToFirst()) {
         filePath = cursor.getString(columnIndex);
     }
     cursor.close();
} else {
     String[] projection = { MediaStore.Images.Media.DATA };
     Cursor cursor = context.getContentResolver().query(contentUri, projection, null , null , null );
     int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
     cursor.moveToFirst();
     filePath = cursor.getString(column_index);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值