Skip to content

Commit 85b951c

Browse files
fix(android): Fix Android Camera1 race condition crash (react-native-camera#2781)
* 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 <[email protected]>
1 parent bfe04aa commit 85b951c

File tree

1 file changed

+16
-9
lines changed
  • android/src/main/java/com/google/android/cameraview

1 file changed

+16
-9
lines changed

android/src/main/java/com/google/android/cameraview/Camera1.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -760,16 +760,23 @@ public void onPictureTaken(byte[] data, Camera camera) {
760760
sound.play(MediaActionSound.SHUTTER_CLICK);
761761
}
762762

763-
if (options.hasKey("pauseAfterCapture") && !options.getBoolean("pauseAfterCapture")) {
764-
camera.startPreview();
765-
mIsPreviewActive = true;
766-
if (mIsScanning) {
767-
camera.setPreviewCallback(Camera1.this);
763+
// our camera might have been released
764+
// when this callback fires, so make sure we have
765+
// exclusive access when restoring its preview
766+
synchronized(Camera1.this){
767+
if(mCamera != null){
768+
if (options.hasKey("pauseAfterCapture") && !options.getBoolean("pauseAfterCapture")) {
769+
mCamera.startPreview();
770+
mIsPreviewActive = true;
771+
if (mIsScanning) {
772+
mCamera.setPreviewCallback(Camera1.this);
773+
}
774+
} else {
775+
mCamera.stopPreview();
776+
mIsPreviewActive = false;
777+
mCamera.setPreviewCallback(null);
778+
}
768779
}
769-
} else {
770-
camera.stopPreview();
771-
mIsPreviewActive = false;
772-
camera.setPreviewCallback(null);
773780
}
774781

775782
isPictureCaptureInProgress.set(false);

0 commit comments

Comments
 (0)