Skip to content
This repository has been archived by the owner on Jan 23, 2024. It is now read-only.

Fix NPE while making the screenshot bitmap mutable #1005

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

nkming2
Copy link

@nkming2 nkming2 commented Oct 29, 2018

sCalibrationImage is null at this point, causing NPE when invoking getConfig()

stack trace:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.Bitmap$Config android.graphics.Bitmap.getConfig()' on a null object reference
	at com.kamron.pogoiv.activities.OcrCalibrationResultActivity.startCalibration(OcrCalibrationResultActivity.java:108)
	at com.kamron.pogoiv.ScreenShotHelper$1.onChange(ScreenShotHelper.java:81)
	at android.database.ContentObserver.onChange(ContentObserver.java:145)
	at android.database.ContentObserver$NotificationRunnable.run(ContentObserver.java:216)
	at android.os.Handler.handleCallback(Handler.java:789)
	at android.os.Handler.dispatchMessage(Handler.java:98)
	at android.os.Looper.loop(Looper.java:164)
	at android.app.ActivityThread.main(ActivityThread.java:6809)
	at java.lang.reflect.Method.invoke(Native Method)
	at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

Copy link
Collaborator

@thearaks thearaks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem can be solved way more easily, see the comments for details.
Thanks for the contribution!

@@ -105,7 +105,11 @@ public static void startCalibration(@NonNull Context context,
sCalibrationImage = bitmap;
} else {
// Make a mutable copy of the bitmap so we can draw on it with a Canvas
sCalibrationImage = bitmap.copy(sCalibrationImage.getConfig() ,true);
sCalibrationImage = bitmap.copy(Bitmap.Config.ARGB_8888 ,true);
if (sCalibrationImage == null) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check is useless since the copy can't return a null bitmap.
Please remove this added lines since they're not helpful.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could be NULL according to the reference:

If the conversion is not supported, or the allocator fails, then this returns NULL

but yea, I think it's a pretty rare case and maybe just doesn't worth bothering at all

@@ -105,7 +105,11 @@ public static void startCalibration(@NonNull Context context,
sCalibrationImage = bitmap;
} else {
// Make a mutable copy of the bitmap so we can draw on it with a Canvas
sCalibrationImage = bitmap.copy(sCalibrationImage.getConfig() ,true);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please replace this line with this instead:

sCalibrationImage = bitmap.copy(bitmap.getConfig(), true);

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's also what I thought originally but I checked the reference and realized getConfig() could actually return NULL.

If the bitmap's internal config is in one of the public formats, return that config, otherwise return null.

Maybe I just make an if there

@@ -105,7 +105,11 @@ public static void startCalibration(@NonNull Context context,
sCalibrationImage = bitmap;
} else {
// Make a mutable copy of the bitmap so we can draw on it with a Canvas
sCalibrationImage = bitmap.copy(sCalibrationImage.getConfig() ,true);
sCalibrationImage = bitmap.copy(Bitmap.Config.ARGB_8888 ,true);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using Bitmap.Config.ARGB_8888 might be a waste of space if the screenshot bitmap used a lower color configuration.
See the previous suggestion to copy the configuration from the screenshot bitmap.

@nkming2
Copy link
Author

nkming2 commented Oct 30, 2018

Amended

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

Successfully merging this pull request may close these issues.

2 participants