From 3d82937795c4ef749c6791dcd9116f32ecb53a85 Mon Sep 17 00:00:00 2001 From: Tim H Date: Tue, 17 Oct 2017 18:37:28 +0200 Subject: [PATCH] prevent app crash caused by failing to reset the auto focus --- .../com/otaliastudios/cameraview/Camera1.java | 26 +++++++++++++------ 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/Camera1.java b/cameraview/src/main/java/com/otaliastudios/cameraview/Camera1.java index 8b85c1d7..57a5f7c0 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/Camera1.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/Camera1.java @@ -40,14 +40,24 @@ class Camera1 extends CameraController { private Runnable mPostFocusResetRunnable = new Runnable() { @Override public void run() { - if (!isCameraAvailable()) return; - mCamera.cancelAutoFocus(); - synchronized (mLock) { - Camera.Parameters params = mCamera.getParameters(); - params.setFocusAreas(null); - params.setMeteringAreas(null); - applyDefaultFocus(params); // Revert to internal focus. - mCamera.setParameters(params); + try { + if (!isCameraAvailable()) return; + mCamera.cancelAutoFocus(); + synchronized (mLock) { + Camera.Parameters params = mCamera.getParameters(); + params.setFocusAreas(null); + params.setMeteringAreas(null); + applyDefaultFocus(params); // Revert to internal focus. + mCamera.setParameters(params); + } + } + catch (Exception e) { + // at least setParameters may fail. + // problem may be device-specific to the Samsung Galaxy J5 + // TODO why does it fail occasionally and is it possible to prevent such errors? + CameraException cameraException = new CameraConfigurationFailedException("Failed to " + + "reset auto focus.", e); + mCameraCallbacks.onError(cameraException); } } };