【问题标题】:Load Image for cropping from Google Photos Gallery从 Google 照片库加载图像以进行裁剪
【发布时间】:2015-05-06 23:15:16
【问题描述】:

是否可以从 Google 相册裁剪图像?我正在创建的应用程序允许用户选择图像并对其进行裁剪。选择图像时,系统会要求用户选择一个应用程序来完成操作(相册、Google 相册等) 从相册中选择和裁剪图像效果很好,但从 Google 相册中选择一个会崩溃且没有错误。 BBM 和 WhatsApp 等应用程序允许从照片中裁剪以上传个人资料图片。我想做类似的事情。

这是我的代码:

Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
Intent i = new Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, PICK_FROM_FILE);

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
    case PICK_FROM_FILE:
    mImageCaptureUri = data.getData();
    Intent intent = new Intent("com.android.camera.action.CROP");
    intent.setType("image/*");

    List<ResolveInfo> list = getPackageManager().queryIntentActivities(intent, 0);

    int size = list.size();

    if (size == 0) {
        Toast.makeText(this, "Can not find image crop app", Toast.LENGTH_SHORT).show();
        return;
    } else {
        intent.setData(mImageCaptureUri);
        intent.putExtra("outputX", 650);
        intent.putExtra("outputY", 650);
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        intent.putExtra("scale", true);
        intent.putExtra("return-data", true);
        Intent i = new Intent(intent);
        ResolveInfo res = list.get(0);
        i.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
        startActivityForResult(i, CROP_FROM_CAMERA);
    }
    break;
    case CROP_FROM_CAMERA:
      Bundle extras = data.getExtras();
      if (extras != null) {
         Bitmap photo = extras.getParcelable("data");
         mImageView.setImageBitmap(photo);
         mImageView.setBackgroundColor(0);
      }
      File f = new File(mImageCaptureUri.getPath());
      if (f.exists()) f.delete();
      break;
    }
}

P.S:这是为 KitKat 和更高版本(API19)

【问题讨论】:

标签: android google-plus crop photos


【解决方案1】:

有以下解决方法:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == CROP_PIC_REQUEST_CODE) {
        if (data != null) {

            Bitmap bitmap = null;

            if(Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
                Bundle extras = data.getExtras();
                bitmap = extras.getParcelable("data");
            }
            else {
                Uri uri = data.getData();

                try {
                    bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(),uri);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if(bitmap!=null) billImg.setImageBitmap(bitmap);
        }
    }
}

这个问题可能是因为,在 android 5.0 以上,Crop 函数在 onActivityResult 中返回一个 URI 而不是 bitmap,所以你必须相应地处理 ti。

无论如何,我认为最好使用像https://github.com/ArthurHub/Android-Image-Cropper 这样的库,它经过优化并可以处理不同的情况。

【讨论】:

    猜你喜欢
    • 2012-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多