在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();
}
这样就直接返回了图片的本地路径