裁剪图像不会成功

Cropping image won't succeed

我正在尝试用相机拍照,然后裁剪它,然后将其显示到 ImageView 中。

不幸的是,您将在下面的代码中看到的 crop 方法中的 startActivityForResult 总是 return 一个错误结果代码。

我想我找到了对此负责的坏蛋,但我不知道如何修复它。

首先是代码:

捕获图像:

/**
 * Starts Intent to open camera and take picture
 */
private void captureImage() {
    Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (cameraIntent.resolveActivity(getPackageManager()) != null) {

        File photoFile = null;

        try {
            photoFile = createImageFile();
        } catch (IOException ex) {
            Toast.makeText(this, "Whoops - could not access external storage!", Toast.LENGTH_SHORT).show();
        }
        if (photoFile != null) {
            Uri photoUri = FileProvider.getUriForFile(this, FILEPROVIDER_AUTHORITY, photoFile);
            contact.setImage(Uri.fromFile(photoFile));
            cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
            try {
                startActivityForResult(cameraIntent, REQUEST_IMAGE_CAPTURE);
            } catch (ActivityNotFoundException e) {
                Toast.makeText(this, "Whoops - something went wrong!", Toast.LENGTH_SHORT).show();
            }
        }
    } else {
        Toast.makeText(this, "Whoops - your device doesn't support capturing images!", Toast.LENGTH_SHORT).show();
    }
}

剪裁图片:

/**
 * Starts (unofficial) Intent to crop chosen image which is likely to fail
 */
private void cropImage() {

    Uri photoUri = FileProvider.getUriForFile(this, FILEPROVIDER_AUTHORITY,new File(contact.getImage().getPath()));
    grantUriPermission("com.android.camera", photoUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);

    Intent cropIntent = new Intent("com.android.camera.action.CROP");
    cropIntent.setDataAndType(photoUri, "image/*");

    cropIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    cropIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

    cropIntent.putExtra("crop", "true");
    cropIntent.putExtra("aspectX", 1);
    cropIntent.putExtra("aspectY", 1);
    cropIntent.putExtra("outputX", 500);
    cropIntent.putExtra("outputY", 500);
    cropIntent.putExtra("outputFormat", "JPEG");
    cropIntent.putExtra("noFaceDetection", true);
    cropIntent.putExtra("return-data", true);
    cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
    try {
        startActivityForResult(cropIntent, REQUEST_IMAGE_CROP);
    } catch (ActivityNotFoundException e) {
        Toast.makeText(this, "Whoops - your device doesn't support the crop action!", Toast.LENGTH_SHORT).show();
    }
}

onActivityResult:

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

    if (resultCode == RESULT_OK) {

        switch (requestCode) {
            case REQUEST_IMAGE_CAPTURE:
                cropImage();
                break;
            case REQUEST_IMAGE_CHOOSE:
                break;
            case REQUEST_IMAGE_CROP:
                Bitmap tmp = data.getExtras().getParcelable("data");
                previewImageView.setImageBitmap(tmp);
                break;
            default:
                super.onActivityResult(requestCode, resultCode, data);
        }
    } else {
        Toast.makeText(this, "Whoops - something went wrong!", Toast.LENGTH_SHORT).show();
    }
}

现在我认为负有责任的坏蛋是 com.android.gallery3d 应用程序,因为每次触发 crop intent 时都会抛出此异常:

com.android.gallery3d W/CropActivity: cannot open region decoder for file: content://de.hska.mycontacts.fileprovider/contact_images/CONTACT_20180413_155111_.jpg
java.io.IOException: Image format not supported
    at android.graphics.BitmapRegionDecoder.nativeNewInstance(Native Method)
    at android.graphics.BitmapRegionDecoder.newInstance(BitmapRegionDecoder.java:124)
    at com.android.gallery3d.filtershow.crop.CropActivity$BitmapIOTask.doInBackground(CropActivity.java:483)
    at com.android.gallery3d.filtershow.crop.CropActivity$BitmapIOTask.doInBackground(CropActivity.java:421)
    at android.os.AsyncTask.call(AsyncTask.java:305)
    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
    at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:243)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
    at java.lang.Thread.run(Thread.java:761) 

com.android.gallery3d W/CropActivity: cannot decode file: content://de.hska.mycontacts.fileprovider/contact_images/CONTACT_20180413_155111_.jpg

我希望有人能帮助我 :)

正如 CommonsWare 已经在评论中指出的那样,Intent com.android.camera.action.CROP 不是官方的,在大多数设备上都不起作用。因此,使用库是更好的做法。

我最终使用了 uCrop,它非常易于使用并且还支持大量自定义。