flutter_image_compress icon indicating copy to clipboard operation
flutter_image_compress copied to clipboard

Out of Memory Error on Android 6.0

Open rbailey91 opened this issue 6 years ago • 14 comments
trafficstars

We are seeing the following error for our users on Android 6 with Samsung devices. Running version 0.6.2.

Fatal Exception: java.lang.OutOfMemoryError: Failed to allocate a 26211852 byte allocation with 16777216 free bytes and 21MB until OOM
       at dalvik.system.VMRuntime.newNonMovableArray(VMRuntime.java)
       at android.graphics.Bitmap.nativeCreate(Bitmap.java)
       at android.graphics.Bitmap.createBitmap + 975(Bitmap.java:975)
       at android.graphics.Bitmap.createBitmap + 946(Bitmap.java:946)
       at android.graphics.Bitmap.createBitmap + 877(Bitmap.java:877)
       at com.example.flutterimagecompress.ext.BitmapCompressExtKt.compress(BitmapCompressExtKt.java)
       at com.example.flutterimagecompress.ext.BitmapCompressExtKt.compress(BitmapCompressExtKt.java)
       at com.example.flutterimagecompress.ext.BitmapCompressExtKt.compress(BitmapCompressExtKt.java)
       at com.example.flutterimagecompress.core.CompressFileHandler$handle$1.run(CompressFileHandler.java)
       at java.util.concurrent.ThreadPoolExecutor.runWorker + 1113(ThreadPoolExecutor.java:1113)
       at java.util.concurrent.ThreadPoolExecutor$Worker.run + 588(ThreadPoolExecutor.java:588)
       at java.lang.Thread.run + 818(Thread.java:818)

Our only call to the library is this :

    var result = await FlutterImageCompress.compressWithFile(file.absolute.path,
        rotate: rotationCorrection);

rbailey91 avatar Aug 06 '19 18:08 rbailey91

Same problem of #76 .

Can you provide a image as an attachment?

CaiJingLong avatar Aug 07 '19 00:08 CaiJingLong

By the way, is there a problem with the same image in ios?

CaiJingLong avatar Aug 07 '19 00:08 CaiJingLong

I don't have an example image as we are seeing this in the wild and not able to reproduce locally as we do not have the device. It is coming through as crash reports from our logging service.

We are not seeing this error at all on IOS. We have seen a number of these crashes and 100% of them have been Android 6 on Samsung devices only.

rbailey91 avatar Aug 07 '19 15:08 rbailey91

OK, thx for issue, I will fix it as soon as possible.

CaiJingLong avatar Aug 08 '19 00:08 CaiJingLong

Because my android 9.0 device does not use a similar error even if I don't use SampleSize, I need human support testing.


dependencies:
  flutter_image_compress: 
    git:
      url: https://github.com/OpenFlutter/flutter_image_compress.git
      ref: 2254a73ef9cfb2458d8cf54902c24d39cc263a49

CaiJingLong avatar Aug 08 '19 07:08 CaiJingLong

If the test passes, I will push this change to the new version.

CaiJingLong avatar Aug 08 '19 07:08 CaiJingLong

Because my android 9.0 device does not use a similar error even if I don't use SampleSize, I need human support testing.

dependencies:
  flutter_image_compress: 
    git:
      url: https://github.com/OpenFlutter/flutter_image_compress.git
      ref: 2254a73ef9cfb2458d8cf54902c24d39cc263a49

For me this solution is not working :( Small images working very well, but on big images the plugin will return null as result.

nilsreichardt avatar Aug 23 '19 14:08 nilsreichardt

I'm unable to provide support testing as this is happening to our users in the wild, we do not have the devices to reproduce it ourselves.

robbie-cronometer avatar Sep 12 '19 20:09 robbie-cronometer

This is my solution, set inSampleSize before compressing

double _getScale(Wallpaper wallpaper) {
    var scale1 = wallpaper.dimension_x / 768;
    var scale2 = wallpaper.dimension_y / 1024;
    return scale1 > scale2 ? scale2 : scale1;
}

await FlutterImageCompress.compressAndGetFile(
    _file.absolute.path,
    _path,
    minWidth: _isLandscape ? 1920 : 1080,
    minHeight: _isLandscape ? 1080 : 1920,
    inSampleSize: _scale.toInt(),
    quality: 88,
);

crazecoder avatar Nov 26 '19 02:11 crazecoder

inSampleSize

Why do you have a parameter called "inSampleSize"? My "compressAndGetFile" looks like that: image

nilsreichardt avatar Dec 26 '19 10:12 nilsreichardt

I found a fix for my problem! 😍

So first of all: The reason for my crash was not the Out of Memory Error. It was FileSystemException: FileSystemException: Cannot open file, path = '.../.flutter-plugins' (OS Error: Permission denied, errno = 13). But the FlutterImageCompress Plugin doesn't catch this exception. So my app crashed. Because of the crash, it was very hard, to find the real exception. Additional this was only a problem for android 10. Finally I figured it out (I named the exception already with permission denied).

In this issue is explained, why the permission denied exception come up: https://github.com/miguelpruivo/flutter_file_picker/issues/169

A workaround for this exception is: Add <application android:requestLegacyExternalStorage="true" to your AndroidManifest.xml.

I hope that I can help some people, who had the same issue :)

nilsreichardt avatar Dec 26 '19 13:12 nilsreichardt

I found a fix for my problem! 😍

So first of all: The reason for my crash was not the Out of Memory Error. It was FileSystemException: FileSystemException: Cannot open file, path = '.../.flutter-plugins' (OS Error: Permission denied, errno = 13). But the FlutterImageCompress Plugin doesn't catch this exception. So my app crashed. Because of the crash, it was very hard, to find the real exception. Additional this was only a problem for android 10. Finally I figured it out (I named the exception already with permission denied).

In this issue is explained, why the permission denied exception come up: miguelpruivo/flutter_file_picker#169

A workaround for this exception is: Add <application android:requestLegacyExternalStorage="true" to your AndroidManifest.xml.

I hope that I can help some people, who had the same issue :)

Let friends who have the same problem try your solution, thanks for replying.

CaiJingLong avatar Dec 27 '19 01:12 CaiJingLong

Same issue here, any update?

algodave avatar Jan 08 '20 09:01 algodave

Stuck in the same place, anybody found a fix?

MohsinN avatar Jan 08 '20 09:01 MohsinN