btastic / flutter_native_image

Native Flutter Image tools
Other
340 stars 138 forks source link

Miui Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference #92

Open rahulserver opened 3 years ago

rahulserver commented 3 years ago

Here is my code:

  File compressedFile = await FlutterNativeImage.compressImage(file.path,
      quality: 25);

This gives below error on miui (Works fine on samsung or android emulator)

E/MethodChannel#flutter_native_image(10761): java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference
E/MethodChannel#flutter_native_image(10761):    at com.example.flutternativeimage.MethodCallHandlerImpl.onMethodCall(MethodCallHandlerImpl.java:48)
E/MethodChannel#flutter_native_image(10761):    at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:262)
E/MethodChannel#flutter_native_image(10761):    at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:178)
E/MethodChannel#flutter_native_image(10761):    at io.flutter.embedding.engine.dart.DartMessenger.lambda$handleMessageFromDart$0$DartMessenger(DartMessenger.java:206)
E/MethodChannel#flutter_native_image(10761):    at io.flutter.embedding.engine.dart.-$$Lambda$DartMessenger$6ZD1MYkhaLxyPjtoFDxe45u43DI.run(Unknown Source:12)
E/MethodChannel#flutter_native_image(10761):    at android.os.Handler.handleCallback(Handler.java:938)
E/MethodChannel#flutter_native_image(10761):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/MethodChannel#flutter_native_image(10761):    at android.os.Looper.loop(Looper.java:236)
E/MethodChannel#flutter_native_image(10761):    at android.app.ActivityThread.main(ActivityThread.java:8031)
E/MethodChannel#flutter_native_image(10761):    at java.lang.reflect.Method.invoke(Native Method)
E/MethodChannel#flutter_native_image(10761):    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:620)
E/MethodChannel#flutter_native_image(10761):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1011)
ammykam commented 2 years ago

I also encountered this problem on many different devices. OPPO, Samsung (S22), Realme

but on ios, it works fine.

JamesMcIntosh commented 2 years ago

@rahulserver @ammykam The following method will return null if the file exists but isn't able to be decoded into a bitmap, perhaps not an image or an unexpected encoding. So in the java code adding a null check and returning an error is probably the best way to handle it. i.e.

Bitmap bmp = BitmapFactory.decodeFile(fileName);
if (bmp == null) {
  result.error("File could not be decoded by BitmapFactory", fileName, null);
  return;
}