0

I've been struggling for days to create an activity that picks a image from the camera and crops it into a square shape efficiently (not loading the image into memory).

Taking the picture is no big deal:

Intent mediaPickingIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
mediaPickingIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);

startActivityForResult(mediaPickingIntent, REQUEST_CODE_GENERIC);

Then, I'm trying to use the OOTB cropping feature for each device like:

String cropDestinationPath = getTemporaryImagePath();
Uri destination = Uri.fromFile(new File(cropDestinationPath));

Intent cropIntent = new Intent("com.android.camera.action.CROP");
cropIntent.setDataAndType(source, "image/*");
cropIntent.putExtra("crop", "true");
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
cropIntent.putExtra("outputX", SQUARE_IMAGE_SIZE);
cropIntent.putExtra("outputY", SQUARE_IMAGE_SIZE);
cropIntent.putExtra("return-data", false);
cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, destination);

startActivityForResult(cropIntent, REQUEST_CODE_IMAGE_CROP);

Now, using the output path of the IMAGE_CAPTURE, I'm able to retrieve the exif data that says the image is rotated (i.e: 270 degrees) but the intent seems to be not caring the exif data.

I'm transforming the resulting path to an uri like:

    private Uri convertPathToContentUri(String imagePath) throws Exception {
            String imageName = null;
            String imageDescription = null;
            String uriString = MediaStore.Images.Media.insertImage(getContentResolver(), imagePath, imageName, imageDescription);
            return Uri.parse(uriString);
    }

BTW: I'm testing this on a Samsung device, same code on Nexus works fine.

Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
Omer
  • 5,470
  • 8
  • 39
  • 64
  • "Taking the picture is no big deal" -- there is no `outputFormat` extra for `ACTION_IMAGE_CAPTURE`. "I'm trying to use the OOTB cropping feature" -- [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 for Android](https://android-arsenal.com/tag/45). Please use one. – CommonsWare Mar 24 '16 at 18:47
  • Thanks. Any specific you can recommend to be used with the camera? – Omer Mar 24 '16 at 19:07
  • Well, you will probably get JPEG by default, as few camera apps support anything else. However, do note that camera apps are buggy, so be sure to have code that sees if the image actually exists at your `EXTRA_OUTPUT` location. Also note that [support for `file:` `Uri` values is going away](https://commonsware.com/blog/2016/03/14/psa-file-scheme-ban-n-developer-preview.html), if that is what you are using for `EXTRA_OUTPUT`. – CommonsWare Mar 24 '16 at 19:10
  • Ha! yes, that's what I'm doing. how do I suppose to provide an output for the camera then? Can't believe this is so complicated... – Omer Mar 24 '16 at 19:20
  • Presumably, we're supposed to use some form of `ContentProvider`, such as `FileProvider`. I haven't tried this for supplying `EXTRA_OUTPUT`, but it's on my to-do list. – CommonsWare Mar 24 '16 at 19:23

0 Answers0