Android 4.3 crop gallery resultCode Cancel

会有一股神秘感。 提交于 2019-11-30 16:09:46

Have you considered just using a library like this one:

https://github.com/biokys/cropimage

I find the com.android.camera.action.CROP can sometimes behave differently from phone to phone and is not always available, so it could cause some problems for you anyway if you are looking to release it.

UPDATE:

I have tested the above library with Android 4.3 and it works with no problem. You just need to add the library to your project.

You can then write your method in a very similar way:

private void performCrop(Uri picUri) {
//you have to convert picUri to string and remove the "file://" to work as a path for this library
String path = picUri.toString().replaceAll("file://", "");

try {
    int aspectX = 750;
    int aspectY = 1011;

    Intent intent = new Intent(this, CropImage.class);
    //send the path to CropImage intent to get the photo you have just taken or selected from gallery
    intent.putExtra(CropImage.IMAGE_PATH, path);

    intent.putExtra(CropImage.SCALE, true);

    intent.putExtra("aspectX", aspectX);
    intent.putExtra("aspectY", aspectY);

    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(mCurrentPhotoPath)));

    startActivityForResult(intent, CROP);
}
catch (ActivityNotFoundException anfe) {
    String errorMessage = "Your device doesn't support the crop action!";
    Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
    toast.show();
}

}

Above library is only useful if your cropping into a lot smaller images. If you want to crop to a better resolution images, it is best to use the Android Crop Intent.

picUri must be a valid URI which points to your image and outputUri should be a new file you created for writing the cropped image. It works on all devices and 4.3 Source code does indeed have com.android.camera.action.CROP intent available for usage. I've tested this on many devices and it works well.

private void performCrop(Uri picUri, Uri outputUri) {
    try {
        int aspectX = 2000;
    int aspectY = 1200;

        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(picUri, "image/*");
        intent.putExtra("scale", "true");
        intent.putExtra("aspectX", aspectX);
        intent.putExtra("aspectY", aspectY);
        intent.putExtra("scaleUpIfNeeded", true);
        intent.putExtra("return-data", false);

        intent.putExtra(MediaStore.EXTRA_OUTPUT, outputUri);

        startActivityForResult(intent, CROP);
    }
    catch (ActivityNotFoundException anfe) {
        String errorMessage = "Your device doesn't support the crop action!";
        Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
        toast.show();
    }
}

I've encountered this problem on a Nexus 10 as well. the crop intent returns a cancelled code. after some tweaking I've found a solution:

In my case the input file set in setDataAndType() was the same file as the output set using the MediaStore.EXTRA_OUTPUT extra. Using the same file for input and output worked fine on most devices, specifically on devices below 4.3. However on 4.3 it would result in canceled crops. Simply using different files for input and output resolved the issue.

So what you need to make sure of is that your picUri parameter points to a file that is not the same as your mCurrentPhotoPath. I'm not sure what exactly changed from 4.2 to 4.3 to cause this issue. But using different files seems to resolve it easily.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!