private void handleImageOnKitKat(Intent data) {
String imagePath = null;
Uri uri = data.getData();
Log.d("HANDLE", "handleImageOnKitKat: uri is " + uri);
if (DocumentsContract.isDocumentUri(this, uri)) {
//如果是document 类型的Uri,则通过document id 处理
String docId = DocumentsContract.getDocumentId(uri);
if ("com.android.providers.media.document".equals(uri.getAuthority())) {
String id = docId.split(":")[1];//解析数字格式id
String selection = MediaStore.Images.Media._ID + "=" + id;
imagePath = getImagePath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection);
}else if ("com.android.providers.downloads.documents".equals(uri.getAuthority())) {
Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(docId));
imagePath = getImagePath(contentUri, null);
}
} else if ("content".equalsIgnoreCase(uri.getScheme())) {
//如果是 content类型的Uri, 则使用普通方法处理
imagePath = getImagePath(uri, null);
}else if ("file".equalsIgnoreCase(uri.getScheme())) {
imagePath = uri.getPath();
}
displayImage(imagePath);//根据图片路径展示图片
}
从相册中选择照片,直接返回了失败
是可以选,但是结果为空吧,应该是你调用到了最近访问之类的,部分手机从最近访问的位置不能获取到文件的真实路径,所以会报错
你找一下拉起相册的方法,看一下是不是有不一样的地方
你找一下拉起相册的方法,加个日志看看
首先 你要确定你的权限 ,6.0要动态申请,7.0还要添加FileProvider,不然是没法成功的。其次再看代码是不是写错了
我猜你是在小米手机上测试的,而且弹出了一个框让你选择相册还是其他媒体,然后下边还有一个选项类似:'下次使用此设置'。。。
如果你不勾选那个选项,直接点相册,则框消失,什么都没发生的样子。也没报错,也没 log,是不是很气啊~
其实你的代码没错,解决方法是:只需要勾选那个选项,再次测试就 Ok了,成功调到了相册页,惊喜不惊喜~