Android手机选取本机图片

在Android应用中,要让用户选择设备上的图片,可以使用Intent.ACTION_GET_CONTENT。本文详细介绍了如何处理返回的Intent,包括处理content数据、file数据以及4.4版后引入的Uri数据,以获取实际图片路径。

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

如果需要从本机图片中选择一张,可以通过Intent.ACTION_GET_CONTENT调出界面,选择后再处理返回的Intent

 

 

调用Intent.ACTION_GET_CONTENT调出选择页面

 

            Intent intent = newIntent(Intent.ACTION_GET_CONTENT);

           intent.setType("image/*");

           activity.startActivityForResult(intent, 1);

 

onActivityResult中

 

情况1——content数据

Intent {dat=content://media/external/images/media/39617 }

 

Uri uri =data.getData();

 

 

String[] proj ={MediaStore.Images.Media.DATA, MediaStore.Images.Media.ORIENTATION};

Cursor cursor =activity.getContentResolver().query(uri, proj, null, null, null);

if (cursor == null)return null;

if(cursor.moveToFirst()) {                        

intdata_index = cursor.getColumnIndexOrThrow(proj[0]);

intorientation_index = cursor.getColumnIndexOrThrow(proj[1]);

cursor.getString(data_index);

cursor.getInt(orientation_index);

}

cursor.close();

 

取出实际路径为  /storage/sdcard0/DCIM/Camera/IMG_20140805_150522.jpg

 

情况2——file数据

 

Intent { dat=file:///storage/sdcard0/MIUI/Gallery/cloud/.thumbnailFile/d495cc46dd7635b4d3c5dde081218c876326e741.jpgtyp=image/jpeg }

 

Uri uri =data.getData();

 

 

 

 

uri.getPath()

取出实际路径为 /storage/sdcard0/MIUI/Gallery/cloud/.thumbnailFile/d495cc46dd7635b4d3c5dde081218c876326e741.jpg

 

 

 

情况3——新版Uri数据

 

4.4引入,如content://com.android.providers.media.documents/document/image:139797,content://com.android.externalstorage.documents/document/image:139797,content://com.android.providers.downloads.documents/document/image:139797

Intent {dat=content://com.android.providers.media.documents/document/image:139797flg=0x1 }

Uri uri =data.getData();

 

 

 

处理代码参考https://github.com/iPaulPro/aFileChooser/blob/master/aFileChooser/src/com/ipaulpro/afilechooser/utils/FileUtils.java

 

对于最近和图像,URI格式为

content://com.android.providers.media.documents/document/image%3A140449

context.getContentResolver().query时的参数

Uri: content: //media/external/images/media

Selection:  _id=?

SelectionArgs:  140449(即imageid

 

对于下载内容,URI格式为content://com.android.providers.downloads.documents/document/692

context.getContentResolver().query时的参数

URI: content://downloads/public_downloads/692

 

对于外部存储,URI格式为content://com.android.externalstorage.documents/document/primary:Mini.jpg

context.getContentResolver().query时的参数

Environment.getExternalStorageDirectory()+ "/" + split[1]极为文件路径

/storage/emulated/0/Mini.jpg

 

 

 

如果编译所用api版本低于4.4,可以采用反射方式尝试调用本地包中接口,如

   public static String getDocumentId(Uri uri){

        String res = null;

        try {

            Class<?> c =Class.forName("android.provider.DocumentsContract");

            Method get =c.getMethod("getDocumentId", Uri.class);

            res =  (String)get.invoke(c, uri);

        } catch (Exception ignored) {

            ignored.getMessage();

        }

        return res;

    }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值