Using “com.android.camera.action.CROP” Intent to c

2019-05-31 08:37发布

问题:

I am using "com.android.camera.action.CROP" to crop images from user`s image gallery..

Crop function:

private void performCrop(String picUri) {
        try {
            Intent cropIntent = new Intent("com.android.camera.action.CROP");

            File f = new File(picUri);
            Uri contentUri = Uri.fromFile(f);

            cropIntent.setDataAndType(contentUri, "image/*");
            cropIntent.putExtra("crop", "false");
            cropIntent.putExtra("aspectX", 1);
            cropIntent.putExtra("aspectY", 1);
            cropIntent.putExtra("outputX", 1024); //512
            cropIntent.putExtra("outputY", 1024); //512

            cropIntent.putExtra("return-data", true);
            startActivityForResult(cropIntent, RESULT_CROP);
        }
        catch (ActivityNotFoundException e) {
            String errorMessage = "your device doesn't support the crop action!";
            Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
            toast.show();
        }
    }

and set the result to image view:

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

        if (requestCode == GALLERY_ACTIVITY_CODE) {
            if(resultCode == Activity.RESULT_OK){
                picturePath = data.getStringExtra("picturePath");

                //perform Crop on the Image Selected from Gallery
                performCrop(picturePath);
            }
        }

        if (requestCode == RESULT_CROP ) {
            if(resultCode == Activity.RESULT_OK){
                Bundle extras = data.getExtras();
                Bitmap selectedBitmap = extras.getParcelable("data");
                // Set The Bitmap Data To ImageView
                addImageImageView.setImageBitmap(selectedBitmap);
                addImageImageView.setScaleType(ImageView.ScaleType.FIT_XY);
            }
        }
    }

On some devices, crop action returns a blurry and small size image (eg. a 4128*2322 pixel image turns to a 160*160 pixel cropped image). While on most devices, it works great. I am not sure what outputX and Y are really do, but changing them doesn`t solve my problem.

回答1:

No, Android Does Not Have a Crop Intent

Many developers are calling startActivity() on an Intent with an action of com.android.camera.action.CROP. They are doing this to crop an image.

This is a really bad idea.

In this specific case, this Intent action is supported by the AOSP Camera app. That app does not exist on all devices. Devices lacking this app will not respond to this undocumented Intent action, and your app will crash.


Solution

There are several open source libraries for cropping images in Android. I have not tried any of them, and therefore cannot vouch for how well any work. However, they are safer bets than relying upon an undocumented Intent action from an app that may not exist on any given user’s device.

Here are some libraries to consider:

  • android-crop Image
  • crop-Image
  • crop-Image (forked from the above one)
  • pick-n-crop

Hope this solved your problem.