Ahmad Arslan
Ahmad Arslan

Reputation: 4528

marshmallow : java.lang.illegalargumentexception: mediaFileUri must be a File Uri

I am having problem in marshmallow device while cropping a photo after capturing it through camera. It is working fine on below 6 version devices. here is my code:

if (requestCode == CAMERA_PICTURE) {

                Intent cropIntent = new Intent("com.android.camera.action.CROP");
                // set data type to be sent , indicate image type and Uri of image
                cropIntent.setDataAndType(mCapturedImageURI, "image/*");
                List<ResolveInfo> list = getPackageManager().queryIntentActivities( cropIntent, 0 );
                int size = list.size();
                // handle the case if there's no cropper in the phone
                if (size == 0) {
                    Toast.makeText(this, "Can not find image crop app", Toast.LENGTH_SHORT).show();
                    return;
                } else {

                   //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);

                    String fileName = Ut.getDateTimeStamp();
                    ContentValues values = new ContentValues();
                    values.put(MediaStore.Images.Media.TITLE, fileName);

                    mCropImageURI = getContentResolver()
                            .insert(
                                    MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                                    values);

                    cropIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                            mCropImageURI);

                    //retrieve data on return
                    cropIntent.putExtra("return-data", true);
                    //start the activity - we handle returning in onActivityResult
                    startActivityForResult(cropIntent, CROP_PICTURE);

                    return;
                }
            } else if (requestCode == CROP_PICTURE) {
                 getCameraPhotoFromIntent(data);
                setImagePathToSource();
            }

Upvotes: 0

Views: 432

Answers (2)

Salmaan
Salmaan

Reputation: 3624

Try this:

    if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.MARSHMALLOW) {
         cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCropImageURI);
    }

Upvotes: 1

Ahmad Arslan
Ahmad Arslan

Reputation: 4528

I have fixed by myself by removing

cropIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                            mCropImageURI);

as nexus 9 use Photos instead of gallery and "Photo" app not allowed Media URI . So by removing this, I am able to crop the image.

Thank you.

Upvotes: 1

Related Questions