0

I am attempting to crop an image using intent after the image is selected from the gallery. Here is my snippet of code

private void showFileChooser() {

        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        //******code for crop image
        intent.putExtra("crop", "true");
        intent.putExtra("aspectX", 0);
        intent.putExtra("aspectY", 0);
        intent.putExtra("return-data", true);

        startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
    }

Here I am calling the above snippet with PICK_IMAGE_REQUEST intent handle

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

        if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {

            try {
                Uri filePath = data.getData();

what could be wrong since I am using the same intent after cropping which is PICK_IMAGE_REQUEST

Blaze
  • 2,269
  • 11
  • 40
  • 82
  • what is the problem? What is not working? – Vladyslav Matviienko Feb 07 '18 at 13:05
  • Do you select the image, but it return the full image instead of oppening a crop screen? – Eduardo Herzer Feb 07 '18 at 13:06
  • You are correct but after cropping it sets the full image to imageView instead of the cropped version at Eduardo Herzer – Blaze Feb 07 '18 at 13:09
  • Well... then CommonsWare answer is right. There is no native support or documented extras for cropping an image. Two options: 1) You should use a library as pointed in answer; 2) If it is not a requirement, you could check if the device supports crop action: if it doesn't, use full image; if it does, use the crop intent (https://stackoverflow.com/a/26358351/2174489 and https://stackoverflow.com/a/39633047/2174489 should help in this case) – Eduardo Herzer Feb 07 '18 at 13:14

1 Answers1

0

There are no documented and supported "crop" extras for ACTION_GET_CONTENT or any other standard Android Intent. Nor is there a documented and supported standard Intent action for cropping. There is no requirement for any device to have apps that support undocumented and unsupported extras, actions, etc.

If you want to crop an image, use any one of a number of existing open source libraries for image cropping.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491