[英]Pick and crop image not working choosing google photos
我選擇一張圖片,但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;
}
}
}
如何解決這個問題呢?
當您選擇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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.