-
Notifications
You must be signed in to change notification settings - Fork 9.8k
[camera] Fix 'getSurface() on a null object reference' Android crash #2871
Conversation
… 'android.view.Surface android.media.ImageReader.getSurface()' on a null object reference"
Hi! I found that your PR includes unformatted code, can you fix them? 😄 /cc @mklim |
Yes, i will fix it soon |
It seems Michael is AFK for a while, let me see if I can find someone for this. |
@@ -421,6 +421,8 @@ public void resumeVideoRecording(@NonNull final Result result) { | |||
} | |||
|
|||
public void startPreview() throws CameraAccessException { | |||
if (pictureImageReader == null || pictureImageReader.getSurface() == null) return; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Drive-by code review....
Under what circumstances is it null? It looks like it gets set in open()
and null'd out in close()
. Is the error you're seeing that the surface is null, and if so, how/when does the surface get populated?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see this error: "android.view.Surface android.media.ImageReader.getSurface() on a null object reference" if the surface is not visible immediately after accepting the camera permissions, and the app crashes. If I reopen the app, I no longer see the error. I think that an extra check for null safety is not bad if it saves from crashes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Gioxxy could you possibly update your pubspec for your app to point to a locally modified camera plugin with heavy instrumentation (Android logs) added, so you can look at the logs and see what order stuff is happening in?
dependency_overrides:
camera:
git:
url: git://github.com/Gioxxy/plugins.git
ref: fix_android_null_surface
path: packages/camera
I'd like to understand how it comes to be that pictureImageReader
or pictureImageReader.getSurface()
gets nulled out - maybe there's a better solution than a simple null check here.
Description
Fix "android.view.Surface android.media.ImageReader.getSurface() on a null object reference"
which causes the app to crash on Android devices immediately after requesting permission to use the camera if the CameraPreview is not visible (example if it is on the second page of a PageView)
Related Issues
flutter/flutter#19595
Checklist
Before you create this PR confirm that it meets all requirements listed below by checking the relevant checkboxes (
[x]
). This will ensure a smooth and quick review process.///
).flutter analyze
) does not report any problems on my PR.Breaking Change
Does your PR require plugin users to manually update their apps to accommodate your change?