Skip to content
This repository has been archived by the owner on May 31, 2023. It is now read-only.

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

Open
rahulserver opened this issue Dec 2, 2021 · 2 comments

Comments

@rahulserver
Copy link

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
Copy link

ammykam commented Jul 4, 2022

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

but on ios, it works fine.

@JamesMcIntosh
Copy link

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants