您的位置:首页 > 编程语言 > Go语言

安卓从googlephoto上选择云端图片导致程序崩溃的解决方案

2017-01-10 14:51 561 查看
当软件选择照片时,如果选择的是使用googlephoto或google云备份过,并在本地删除过的图片时,程序就会崩溃或图片是空白 ,报错原因:

IllegalArgumentException:InvalidURI:content://com.google.android.apps.photos.contentprovider/0/1‌​/mediaKey%3A...6mkQk‌​-P4tzU/ACTUAL/11...8‌​0


原因是使用googlephoto备份过并在本地删除的图片会在手机中存储一个缩略图,其他软件调用googlephoto选择图片时依然能看到删除过的图片,但是此时图片的url已经不是本地的url了,而是一个图片的下载链接,这时使用getPath(url)会得到一个null路径。

国内的好多软件都存在这个问题,其实解决办法也很简单,就是通过在onactivityresult中把data.getData()得到的数据存到一个临时文件中做一个拷贝,再读取就可以了。

选择图片时要使用
Intent.ACTION_GET_CONTENT


Intent selectPhoto = new Intent(Intent.ACTION_GET_CONTENT,MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
selectPhoto.setType("image/*");
startActivityForResult(selectPhoto,ONACTIVITYRESULT_SELECT_PHOTO);


//onActivityResult

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != RESULT_OK) {
PictureManager.this.finish();
return;
} else {
switch (requestCode) {
case BabyCareStaticConstant.ONACTIVITYRESULT_SELECT_PHOTO:// select
// 创建空文件用于保存图片数据
File tempFile = new File(this.getFilesDir().getAbsolutePath(), "temp_image");

//拷贝uri内容到空文件中
try {
tempFile.createNewFile();
copyAndClose(this.getContentResolver().openInputStream(data.getData()),new FileOutputStream(tempFile));
} catch (IOException e) {
//Log Error
}

//Now fetch the new URI
Uri pickedUri = Uri.fromFile(tempFile);
String imgPath = pickedUri.toString();
if (imgPath.indexOf("file://") > -1) {
// 如果是从文件管理器读取的绝对路径,直接删掉开头的"file://"即可
imgPath = imgPath.replace("file://", "");
} else if (imgPath.indexOf("content://") > -1) {
// 如果是从相册中读取的相对路径,则需要转换成绝对路径
imgPath = BitmapHelper.getRealPathFromURI(
PictureManager.this, pickedUri);
}
returnToFirstActivity(imgPath, new PictureHelper().imageName);

break;

}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android 谷歌 图片
相关文章推荐