flutter_native_image icon indicating copy to clipboard operation
flutter_native_image copied to clipboard

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

Open rahulserver opened this issue 4 years ago • 2 comments

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)

rahulserver avatar Dec 02 '21 05:12 rahulserver

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

but on ios, it works fine.

ammykam avatar Jul 04 '22 09:07 ammykam

@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;
}

JamesMcIntosh avatar Nov 11 '22 23:11 JamesMcIntosh