2

I'm trying to capture an image from the Android Camera/ Pick an image from the gallery and then crop it before performing other operations on it. I'm having trouble with getting back the URI of the cropped image. Any help on how to get back the URI of the image once it has been cropped would be much appreciated!

The code below pertains to my onActivityResult and my function performing the crop

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == SELECT_FILE || requestCode == REQUEST_IMAGE_CAPTURE) {
            if(!IS_CAMERA_USED) {
                selectedImageUri = data.getData();
            }
            performCrop();
        }
        else if(requestCode == CROP_IMAGE){
            selectedImageUri = data.getData();
            onSelectedImageResult();
        }
    }
}



    public void performCrop() {
    // take care of exceptions
    Display display = getWindowManager().getDefaultDisplay();
    try {
        // call the standard crop action intent (the user device may not
        // support it)
        Intent cropIntent = new Intent("com.android.camera.action.CROP");
        // indicate image type and Uri
        cropIntent.setDataAndType(selectedImageUri, "image/*");
        // set crop properties
        cropIntent.putExtra("crop", "true");
        // indicate aspect of desired crop
        cropIntent.putExtra("aspectY", 1);
        if(IS_PROFILE_PICTURE) {
            cropIntent.putExtra("aspectX", 1);
        }
        else {
            cropIntent.putExtra("aspectX", 2);
        }
        // indicate output X and Y
        cropIntent.putExtra("outputX", display.getWidth());
        cropIntent.putExtra("outputY", display.getHeight());
        // retrieve data on return
        cropIntent.putExtra("return-data", true);
        // start the activity - we handle returning in onActivityResult
        startActivityForResult(cropIntent, CROP_IMAGE);
    }
    // respond to users whose devices do not support the crop action
    catch (ActivityNotFoundException anfe) {
        Toast toast = Toast
                .makeText(this, "This device doesn't support the crop action!", Toast.LENGTH_SHORT);
        toast.show();
    }
}

The problem is in the line selectedImageUri = data.getData(), where data.getData() returns null after having done the crop. How do I get back the URI of the cropped image? I don't want to get data.getExtras.getParcelable("data") as that returns the thumbnail and ruins the image resolution.

Thanks in advance!

Vidhyasagar
  • 153
  • 3
  • 11
  • [Android does not have a `CROP` `Intent`](https://commonsware.com/blog/2013/01/23/no-android-does-not-have-crop-intent.html). There are many [image cropping libraries available for Android](https://android-arsenal.com/tag/45). Please use one. – CommonsWare Sep 27 '16 at 18:24

3 Answers3

0

You can get the image bitmap with this code:

if (requestCode == CROP_IMAGE) {
    if (data != null) {
        Bundle extras = data.getExtras();
        if (extras != null) {
            Bitmap selectedBitmap = extras.getParcelable("data");

            //imgView.setImageBitmap(selectedBitmap);
        }
    }
}

URI is not possible I think, because the cropped image is not saved on the storage.

Denny Weinberg
  • 2,492
  • 1
  • 21
  • 34
0

Thanks for all your answers, but I found something way simpler to incorporate and use that avoids all hassles due to the `com.android.camera.action.CROP' class, and it can be found here

Vidhyasagar
  • 153
  • 3
  • 11
0

I think that you should save the cropped Bitmap to your storage, and then use the URI of the cropped Bitmap to performe something over it! I know it's not a professional work but at least it should do it