我选择一张图片,只有Google照片返回,找不到商品。
这是创建用于选择照片的意图的代码:
Intent i = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i,REQUEST_CODE_FROM_GALLERY);
遵循我的activityresult
代码:
@Override
public void onActivityResult(int requestCode, int resultCode, final Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
switch (requestCode) {
case REQUEST_CODE_FROM_GALLERY:
Utils.toast_center(Main_Theme.this,""+data.getData());
try {
String root = Environment.getExternalStorageDirectory().toString();
File mydir = new File(root + "/Image");
mydir.mkdirs();
String fname = "user_image.jpg";
final File file = new File(mydir, fname);
Intent intent1 = new Intent("com.android.camera.action.CROP");
intent1.setType("image/*");
intent1.setData(data.getData()); // Uri to the image you want to crop
intent1.putExtra("scale", true);
intent1.putExtra("circleCrop", new String(""));
intent1.putExtra("return-data", false);
intent1.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
startActivityForResult(intent1, 03);
}catch (ActivityNotFoundException e){
Utils.toast_center(Main_Theme.this,"Something Went Error. Try Again");
}
break;
case 03:
File filee = new File(Environment.getExternalStorageDirectory().toString() + "//Image/user_image.jpg");
Bitmap bmp = BitmapFactory.decodeFile(filee.getPath());
iconn.setImageBitmap(bmp);
settheame();
break;
}
}
}
如何解决这个问题?
答案 0 :(得分:0)
当您选择Google照片图像时,URI开始就像"content://com.google.android.apps.photos.content/"
,而不是来自本地设备。
本地uri,例如content://media/external/images/media/49518
因此,您需要首先检查URI,然后基于该URI从服务器或本地图像中获取图像。
您可以使用DocumentsProvider API。
private Bitmap getBitmapFromUri(Uri uri) throws IOException
{
ParcelFileDescriptor parcelFileDescriptor =
getContentResolver().openFileDescriptor(uri, "r");
FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
Bitmap image = BitmapFactory.decodeFileDescriptor(fileDescriptor);
parcelFileDescriptor.close();
return image;
}