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

Commit

Permalink
fix(android): Fix Android Camera1 race condition crash (#2781)
Browse files Browse the repository at this point in the history
* add synchronized to take pictures callback in case camera was already stopped.

This will also prevent a possible null camera object.

* use our mCamera variable instead of the one received in the callback so we know if it has been disposed.

Co-authored-by: Cristiano Coelho <cristianocca@hotmail.com>
  • Loading branch information
cristianoccazinsp and cristianocca committed Apr 8, 2020
1 parent bfe04aa commit 85b951c
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions android/src/main/java/com/google/android/cameraview/Camera1.java
Original file line number Diff line number Diff line change
Expand Up @@ -760,16 +760,23 @@ public void onPictureTaken(byte[] data, Camera camera) {
sound.play(MediaActionSound.SHUTTER_CLICK);
}

if (options.hasKey("pauseAfterCapture") && !options.getBoolean("pauseAfterCapture")) {
camera.startPreview();
mIsPreviewActive = true;
if (mIsScanning) {
camera.setPreviewCallback(Camera1.this);
// our camera might have been released
// when this callback fires, so make sure we have
// exclusive access when restoring its preview
synchronized(Camera1.this){
if(mCamera != null){
if (options.hasKey("pauseAfterCapture") && !options.getBoolean("pauseAfterCapture")) {
mCamera.startPreview();
mIsPreviewActive = true;
if (mIsScanning) {
mCamera.setPreviewCallback(Camera1.this);
}
} else {
mCamera.stopPreview();
mIsPreviewActive = false;
mCamera.setPreviewCallback(null);
}
}
} else {
camera.stopPreview();
mIsPreviewActive = false;
camera.setPreviewCallback(null);
}

isPictureCaptureInProgress.set(false);
Expand Down

0 comments on commit 85b951c

Please sign in to comment.