add try catch to catch exceptions when takePicture fails (#1024)

* add try catch to catch exceptions when takePicture fails, to give the user a better experience than an app crash

* do not log errors, as they are alreayd logged by the camera engine, and do not put try catch around DNG captures

Co-authored-by: Mattia Iavarone <mat.iavarone@gmail.com>
pull/1030/head
Michael 4 years ago committed by GitHub
parent 0f6ab20c3e
commit 5d10277d7b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 7
      cameraview/src/main/java/com/otaliastudios/cameraview/picture/Full1PictureRecorder.java
  2. 7
      cameraview/src/main/java/com/otaliastudios/cameraview/picture/Full2PictureRecorder.java

@ -1,10 +1,12 @@
package com.otaliastudios.cameraview.picture; package com.otaliastudios.cameraview.picture;
import android.hardware.Camera; import android.hardware.Camera;
import android.util.Log;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.exifinterface.media.ExifInterface; import androidx.exifinterface.media.ExifInterface;
import com.otaliastudios.cameraview.CameraException;
import com.otaliastudios.cameraview.PictureResult; import com.otaliastudios.cameraview.PictureResult;
import com.otaliastudios.cameraview.engine.Camera1Engine; import com.otaliastudios.cameraview.engine.Camera1Engine;
import com.otaliastudios.cameraview.engine.offset.Reference; import com.otaliastudios.cameraview.engine.offset.Reference;
@ -44,6 +46,7 @@ public class Full1PictureRecorder extends FullPictureRecorder {
// or takePicture can hang and leave the camera in a bad state. // or takePicture can hang and leave the camera in a bad state.
mCamera.setPreviewCallbackWithBuffer(null); mCamera.setPreviewCallbackWithBuffer(null);
mEngine.getFrameManager().release(); mEngine.getFrameManager().release();
try {
mCamera.takePicture( mCamera.takePicture(
new Camera.ShutterCallback() { new Camera.ShutterCallback() {
@Override @Override
@ -95,6 +98,10 @@ public class Full1PictureRecorder extends FullPictureRecorder {
} }
); );
LOG.i("take() returned."); LOG.i("take() returned.");
} catch (Exception e) {
mError = e;
dispatchResult();
}
} }
@Override @Override

@ -8,6 +8,7 @@ import android.hardware.camera2.TotalCaptureResult;
import android.media.Image; import android.media.Image;
import android.media.ImageReader; import android.media.ImageReader;
import android.os.Build; import android.os.Build;
import android.util.Log;
import com.otaliastudios.cameraview.PictureResult; import com.otaliastudios.cameraview.PictureResult;
import com.otaliastudios.cameraview.controls.PictureFormat; import com.otaliastudios.cameraview.controls.PictureFormat;
@ -86,7 +87,13 @@ public class Full2PictureRecorder extends FullPictureRecorder
public void onCaptureCompleted(@NonNull ActionHolder holder, public void onCaptureCompleted(@NonNull ActionHolder holder,
@NonNull CaptureRequest request, @NonNull CaptureRequest request,
@NonNull TotalCaptureResult result) { @NonNull TotalCaptureResult result) {
try {
super.onCaptureCompleted(holder, request, result); super.onCaptureCompleted(holder, request, result);
} catch (Exception e) {
mError = e;
dispatchResult();
}
if (mResult.format == PictureFormat.DNG) { if (mResult.format == PictureFormat.DNG) {
mDngCreator = new DngCreator(holder.getCharacteristics(this), result); mDngCreator = new DngCreator(holder.getCharacteristics(this), result);
mDngCreator.setOrientation(ExifHelper.getExifOrientation(mResult.rotation)); mDngCreator.setOrientation(ExifHelper.getExifOrientation(mResult.rotation));

Loading…
Cancel
Save