Editing is not supported for this Image in Oreo Version problem

Viewed 4171

Editing is not supported for this Image in Oreo Version problem.

(Editing is not supported for this Image)this Toast showing when select the Image from gallery in Oreo Version mobile. I already asked this question but no one reply me. Please check my code and revert back as soon as possible.

This is my code:-

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

    if (resultCode != 0) {
        if (requestCode == GALLERY_CAPTURE) {              
            picUri = data.getData();               
            String[] filePathColumn = {MediaStore.Images.Media.DATA};
            Cursor cursor = getContext().getContentResolver().query(picUri, filePathColumn, null, null, null);
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);            
            performCrop();
        }            
        else if (requestCode == CROP_PIC) {             
            Bundle extras = data.getExtras();            
            Bitmap thePic = extras.getParcelable("data");
            rimage.setImageBitmap(thePic);
            getCroppedBitmap(thePic);
            Uri tempUri = getImageUri(getActivity(), thePic);
            finalFile = new File(getRealPathFromURI(tempUri));
            Log.e("cropped Image Path", String.valueOf(finalFile));
        }
    }
}

public Uri getImageUri(Context inContext, Bitmap inImage) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    inImage.compress(Bitmap.CompressFormat.PNG, 100, bytes);
    String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
    return Uri.parse(path);
}

public String getRealPathFromURI(Uri uri) {
    Cursor cursor = getContext().getContentResolver().query(uri, null, null, null, null);
    cursor.moveToFirst();
    int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
    return cursor.getString(idx);
}

public Bitmap getCroppedBitmap(Bitmap bitmap) {
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
            bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);
    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2,
            bitmap.getWidth() / 2, paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);
    return output;
}

private void performCrop() {        
    try {            
        Intent cropIntent = new Intent("com.android.camera.action.CROP");            cropIntent.setClassName("com.prince","com.prince.RegisterStepThree");           
        cropIntent.setDataAndType(picUri, "image/*");         
        cropIntent.putExtra("crop", "true");           
        cropIntent.putExtra("aspectX", 1);
        cropIntent.putExtra("aspectY", 1);            
        cropIntent.putExtra("outputX", 96);
        cropIntent.putExtra("outputY", 96);         
        cropIntent.putExtra("return-data", true);             
        startActivityForResult(cropIntent, CROP_PIC);
    }       
    catch (ActivityNotFoundException anfe) {
        Toast toast = Toast
                .makeText(getContext(), "This device doesn't support the crop action!", Toast.LENGTH_SHORT);
        toast.show();
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}
3 Answers

Add the Uri to the file to the intent

intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(<file object>);

I also tried passing the Uri generated by FileProvider.getUriForFile(<file object>) and that worked.

Using intent.putExtra(MediaStore.EXTRA_OUTPUT, uri) looks like the right answer in several actions including ACTION_EDIT and MediaStore.ACTION_IMAGE_CAPTURE.

Notice that in modern API the URI should be created using FileProvider

Here is code to crop image based on file path. I have tested with android 11 also and it's working fine. You can also see the result in below screen.

Kotlin function written in Utils.kt File:

fun performCrop(picUri: File, f: Fragment) {
val packageName: String = f.requireContext().applicationContext.packageName
val authority = "$packageName.fileprovider"

val uri = FileProvider.getUriForFile(f.requireContext(), authority, picUri)
if (Build.VERSION.SDK_INT >= 24) {
    try {
        val m = StrictMode::class.java.getMethod("disableDeathOnFileUriExposure")
        m.invoke(null)
    } catch (e: Exception) {
        e.printStackTrace()
    }
}
try {
    val cropIntent = Intent("com.android.camera.action.CROP")
    cropIntent.setDataAndType(uri, "image/*")
    cropIntent.putExtra("crop", true)
    cropIntent.putExtra("aspectX", 1)
    cropIntent.putExtra("aspectY", 1)
    cropIntent.putExtra("outputX", 512)
    cropIntent.putExtra("outputY", 512)
    cropIntent.putExtra("return-data", true)
    cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(picUri))
    cropIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
    cropIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
    f.startActivityForResult(
        Intent.createChooser(
            cropIntent,
            "Complete action using"
        ), Constants.REQUEST_CODE_CROP
    )
}
catch (anfe: ActivityNotFoundException) {
    val errorMessage = "Whoops - your device doesn't support the crop action!"
    val toast = Toast.makeText(f.requireContext(), errorMessage, Toast.LENGTH_SHORT)
    toast.show()
} }

Call above function from EditProfile Fragment:

performCrop(File(editProfileViewModel.filePath), this@EditProfile)

Step1: Select Image from Gallery Use third party library to fetch images from gallary

Step2: Crop image as per need Use Intent.crop

Step3: Set Image in Imageview enter image description here

Related