Android 4.3 crop gallery resultCode Cancel

限于喜欢 提交于 2019-12-03 20:25:15

问题


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 !


回答1:


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

}




回答2:


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



回答3:


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.



来源:https://stackoverflow.com/questions/17930577/android-4-3-crop-gallery-resultcode-cancel

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