Android 4.3 crop gallery resultCode Cancel

androidcropgallery

My Galaxy Nexus is now running on Android 4.3 allowing me to test my application with this new version. Everything seems fine except cropping.

I have an application that uses the camera to take picture and then crop the image through the gallery app.

I am also able to choose a picture from the gallery and crop it after.
Since Android 4.3, the gallery application changed.

If i take a picture with the camera api and then ask the gallery to crop it in my onActivityResult method the resultCode is set to 0 (meaning cancel) whereas i clicked on "Save" from the crop view.

But if i choose a picture from the gallery and crop it everything works, the resultCode parameter is set to -1.
I call the same method to crop the picture in both cases.

I have quickpic (an alternative to the gallery app) on my phone, with it everything works !

private void performCrop(Uri picUri) {
    try {
        int aspectX = 750;
        int aspectY = 1011;

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

        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();
    }
}

Everything worked fine on Android 4.2.2.
Thank you for your help !

Best Answer

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();
}

}