flutter_image_cropper icon indicating copy to clipboard operation
flutter_image_cropper copied to clipboard

PlatformException(crop_error, y + height must be <= bitmap.height(), java.lang.IllegalArgumentException: y + height must be <= bitmap.height()

Open dharambudh1 opened this issue 1 year ago • 2 comments

I'm encountering an issue after cropping an image on the Android side.

PlatformException(crop_error, y + height must be <= bitmap.height(), java.lang.IllegalArgumentException: y + height must be <= bitmap.height()
	at android.graphics.Bitmap.createBitmap(Bitmap.java:924)
	at android.graphics.Bitmap.createBitmap(Bitmap.java:880)
	at com.yalantis.ucrop.task.BitmapCropTask.crop(BitmapCropTask.java:165)
	at com.yalantis.ucrop.task.BitmapCropTask.doInBackground(BitmapCropTask.java:105)
	at com.yalantis.ucrop.task.BitmapCropTask.doInBackground(BitmapCropTask.java:37)
	at android.os.AsyncTask$3.call(AsyncTask.java:394)
	at java.util.concurrent.FutureTask.run(FutureTask.java:264)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:644)
	at java.lang.Thread.run(Thread.java:1012)
, null)

Here are the steps to reproduce the issue:

  1. Set the aspect ratio to 16:9.
  2. Select an image.
  3. Drag the crop box down to the very bottom of the image.
  4. Tap the checkmark button in the top-right corner.

Video footage:

https://github.com/user-attachments/assets/3ac65921-3e74-477a-bc07-5eebb6ec6b2a

My code:

  Future<CroppedFile?> cropImage({
    required String sourcePath,
    CropAspectRatioPreset preset = CropAspectRatioPreset.original,
  }) async {
    CroppedFile? croppedFile;

    try {
      final (int, int)? value = getCropAspectRatioPreset(preset);
      final double ratioX = double.parse((value?.$1 ?? "0.0").toString());
      final double ratioY = double.parse((value?.$2 ?? "0.0").toString());
      croppedFile = await cropper.cropImage(
        sourcePath: sourcePath,
        aspectRatio: value != null
            ? CropAspectRatio(ratioX: ratioX, ratioY: ratioY)
            : null,
      );
      AppLogger().info(message: "cropImage() file: ${croppedFile?.path ?? ""}");
    } on Exception catch (err, stack) {
      AppLogger().error(message: "Exception caught", err: err, stack: stack);
    } finally {}

    return Future<CroppedFile?>.value(croppedFile);
  }

  (int, int)? getCropAspectRatioPreset(CropAspectRatioPreset preset) {
    (int, int)? value;

    switch (preset) {
      case CropAspectRatioPreset.square:
        value = (1, 1);
        break;
      case CropAspectRatioPreset.ratio3x2:
        value = (3, 2);
        break;
      case CropAspectRatioPreset.ratio4x3:
        value = (4, 3);
        break;
      case CropAspectRatioPreset.ratio5x3:
        value = (5, 3);
        break;
      case CropAspectRatioPreset.ratio5x4:
        value = (5, 4);
        break;
      case CropAspectRatioPreset.ratio7x5:
        value = (7, 5);
        break;
      case CropAspectRatioPreset.ratio16x9:
        value = (16, 9);
        break;
      case CropAspectRatioPreset.original:
        value = null;
        break;
    }

    return value;
  }

@hnvn Could you please take a look at this? This library is a critical component of our project.

dharambudh1 avatar Nov 29 '24 12:11 dharambudh1

Also received this error on a Pixel 8 running Android 15

DanMossa avatar Mar 07 '25 00:03 DanMossa

Same issue there, any news ?

Shazamepool avatar Aug 27 '25 07:08 Shazamepool