裁剪图像不会成功

红色代码

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

不幸的是,您在下面的代码中看到的crop方法中的startActivityForResult将始终返回错误结果代码。

我认为我找到了造成这种情况的坏蛋,但我不知道如何解决它。

首先是代码:

拍摄图像:

/**
 * 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应用程序,原因是每次触发作物意图时都会引发此异常:

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$2.call(AsyncTask.java:305)
    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
    at android.os.AsyncTask$SerialExecutor$1.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在评论中所述,Intentcom.android.camera.action.CROP不是官方的,在大多数设备上都无法使用。因此,使用库是一种更好的做法。

我最终使用了真正易于使用的uCrop,并且还支持许多自定义。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章