From db7b9e4a98f7515cacbc3cd135304fae507147a5 Mon Sep 17 00:00:00 2001 From: cristianoccazinsp <48869228+cristianoccazinsp@users.noreply.github.com> Date: Tue, 31 Dec 2019 12:30:01 -0300 Subject: [PATCH] fix(android): android crash fixes. Make sure no unsupported aspect ratio is used, and do not crash when there are no cameras available. (#2662) --- .../google/android/cameraview/Camera1.java | 26 ++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/android/src/main/java/com/google/android/cameraview/Camera1.java b/android/src/main/java/com/google/android/cameraview/Camera1.java index 3a2fff31f..7130ae7b8 100644 --- a/android/src/main/java/com/google/android/cameraview/Camera1.java +++ b/android/src/main/java/com/google/android/cameraview/Camera1.java @@ -446,6 +446,7 @@ boolean setAspectRatio(final AspectRatio ratio) { final Set sizes = mPreviewSizes.sizes(ratio); if (sizes == null) { // do nothing, ratio remains unchanged. Consistent with Camera2 and initial mount behaviour + Log.w("CAMERA_1::", "setAspectRatio received an unsupported value and will be ignored."); } else { mAspectRatio = ratio; mBgHandler.post(new Runnable() { @@ -870,7 +871,10 @@ private void chooseCamera() { if(_mCameraId == null){ int count = Camera.getNumberOfCameras(); if(count == 0){ - throw new RuntimeException("No camera available."); + //throw new RuntimeException("No camera available."); + mCameraId = INVALID_CAMERA_ID; + Log.w("CAMERA_1::", "getNumberOfCameras returned 0. No camera available."); + return; } for (int i = 0; i < count; i++) { @@ -899,19 +903,38 @@ private boolean openCamera() { if (mCamera != null) { releaseCamera(); } + + // in case we got an invalid camera ID + // due to no cameras or invalid ID provided, + // return false so we can raise a mount error + if(mCameraId == INVALID_CAMERA_ID){ + return false; + } + try { mCamera = Camera.open(mCameraId); mCameraParameters = mCamera.getParameters(); + // Supported preview sizes mPreviewSizes.clear(); for (Camera.Size size : mCameraParameters.getSupportedPreviewSizes()) { mPreviewSizes.add(new Size(size.width, size.height)); } + // Supported picture sizes; mPictureSizes.clear(); for (Camera.Size size : mCameraParameters.getSupportedPictureSizes()) { mPictureSizes.add(new Size(size.width, size.height)); } + + // to be consistent with Camera2, and to prevent crashes on some devices + // do not allow preview sizes that are not also in the picture sizes set + for (AspectRatio aspectRatio : mPreviewSizes.ratios()) { + if (mPictureSizes.sizes(aspectRatio) == null) { + mPreviewSizes.remove(aspectRatio); + } + } + // AspectRatio if (mAspectRatio == null) { mAspectRatio = Constants.DEFAULT_ASPECT_RATIO; @@ -939,6 +962,7 @@ private AspectRatio chooseAspectRatio() { void adjustCameraParameters() { SortedSet sizes = mPreviewSizes.sizes(mAspectRatio); if (sizes == null) { // Not supported + Log.w("CAMERA_1::", "adjustCameraParameters received an unsupported aspect ratio value and will be ignored."); mAspectRatio = chooseAspectRatio(); sizes = mPreviewSizes.sizes(mAspectRatio); }