3

For Cropping images taken from android phone in Lollipop and above One should use File Provider here is my code.

<provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="com.example.test.fileprovider"
            android:grantUriPermissions="true"
            android:exported="false"
            >
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/filepaths"
                />

        </provider>

<paths>
    <external-path name="myexternalimages" path="dcim/ProfileImage/"/>
</paths>





 final  Uri providedUri = FileProvider.getUriForFile(ProfileUpdateActivity.this,
                    "com.example.test.fileprovider", imageUpLoadFile);
            Intent cropIntent = new Intent("com.android.camera.action.CROP");



            //indicate image type and Uri
            cropIntent.setDataAndType(providedUri, "image/*");
            //set crop properties
            cropIntent.putExtra("crop", "true");
            //indicate aspect of desired crop
            cropIntent.putExtra("aspectX", 1);
            cropIntent.putExtra("aspectY", 1);
            //indicate output X and Y
            cropIntent.putExtra("outputX", 256);
            cropIntent.putExtra("outputY", 256);
            //retrieve data on return
            cropIntent.putExtra("return-data", true);

            // Exception will be thrown if read permission isn't granted
            cropIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

            startActivityForResult(cropIntent, PIC_CROP);

where imageUploadFile =/storage/emulated/0/dcim/ProfileImage/IMG_20160330_134823_1697877403.png (Example)

Now When it returns successfully onActivityResult it I get an error toast message as Editing this image is not available

Sutirth
  • 1,217
  • 4
  • 15
  • 26

1 Answers1

1

Adding EXTRA_OUTPUT to the intent fixed it for me.

cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, providedUri);  

But make sure to also add write uri permission to the Intent.
So that the Camera app can write the cropped image to the provided Uri.

cropIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
ElegyD
  • 4,393
  • 3
  • 21
  • 37