diff --git a/library/src/main/api21/com/google/android/cameraview/Camera2.java b/library/src/main/api21/com/google/android/cameraview/Camera2.java index 4835f89e..e21fdfce 100644 --- a/library/src/main/api21/com/google/android/cameraview/Camera2.java +++ b/library/src/main/api21/com/google/android/cameraview/Camera2.java @@ -132,6 +132,9 @@ public void onClosed(@NonNull CameraCaptureSession session) { @Override public void onPrecaptureRequired() { + if (mPreviewRequestBuilder == null || mCaptureSession == null) { + return; + } mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER, CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER_START); setState(STATE_PRECAPTURE); @@ -580,6 +583,10 @@ void updateFlash() { * Locks the focus as the first step for a still image capture. */ private void lockFocus() { + if (mPreviewRequestBuilder == null || mCaptureCallback == null || mCaptureSession == null) { + return; + } + mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CaptureRequest.CONTROL_AF_TRIGGER_START); try { @@ -635,16 +642,18 @@ void captureStillPicture() { mDisplayOrientation * (mFacing == Constants.FACING_FRONT ? 1 : -1) + 360) % 360); // Stop preview and capture a still picture. - mCaptureSession.stopRepeating(); - mCaptureSession.capture(captureRequestBuilder.build(), - new CameraCaptureSession.CaptureCallback() { - @Override - public void onCaptureCompleted(@NonNull CameraCaptureSession session, - @NonNull CaptureRequest request, - @NonNull TotalCaptureResult result) { - unlockFocus(); - } - }, null); + if (mCaptureSession != null) { + mCaptureSession.stopRepeating(); + mCaptureSession.capture(captureRequestBuilder.build(), + new CameraCaptureSession.CaptureCallback() { + @Override + public void onCaptureCompleted(@NonNull CameraCaptureSession session, + @NonNull CaptureRequest request, + @NonNull TotalCaptureResult result) { + unlockFocus(); + } + }, null); + } } catch (CameraAccessException e) { Log.e(TAG, "Cannot capture a still picture.", e); } @@ -655,6 +664,10 @@ public void onCaptureCompleted(@NonNull CameraCaptureSession session, * capturing a still picture. */ void unlockFocus() { + if (mPreviewRequestBuilder == null || mCaptureCallback == null || mCaptureSession == null) { + return; + } + mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CaptureRequest.CONTROL_AF_TRIGGER_CANCEL); try {