android手机调用系统相册(兼容小米手机)

本文介绍了在Android项目中遇到的小米手机调用系统相册时出现的选择器问题及图片路径获取方法。针对小米手机特有的数据格式,提供了一种处理方案。

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

在android项目中我们往往会选择照片 , 比较懒的方式肯定是直接调用系统相册, 当然你也可以自己写一个相册读取 , 总结下我在项目中遇到的问题吧 , 目前是发现小米手机调用系统相册竟然弹出来一个选择器 , 如图
小米手机调用系统相册弹出框
而小米手机在选择照片回调intent的数据格式也是和其他不一样的(目前我也发现miui , 如果有其他手机类型欢迎评论)

  • 一般情况下我们根据返回的uri来获取图片途径
public static String getFilePathByFileUri(Context context, Uri uri) {
        String filePath = null;
        Cursor cursor = context.getContentResolver().query(uri, null, null,
                null, null);
        if (cursor.moveToFirst()) {
            filePath = cursor.getString(cursor
                    .getColumnIndex(MediaStore.Images.Media.DATA));
        }
        cursor.close();
        return filePath;
    }

但是小米手机你会发现这种crash错误java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=-1, data=Intent { dat=file:///storage/sdcard0/Tencent/QQ_Images/null-729086946e14ea5f.jpg typ=image/jpeg }} to activity
对于这种情况我是这样处理的

                 Uri  uri = data.getData();
                     String type = data.getType();  
                     if (uri.getScheme().equals("file") && (type == null||type.contains("image/")) ) {


                         imagePath = uri.getEncodedPath();  
                     }

这样就直接返回了图片的本地路径

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值