Skip to content

Commit e7ed948

Browse files
Merge pull request react-native-camera#2768 from cristianoccazinsp/android-crash-camerainfo
Crash fix when getCameraInfo fails
2 parents d1e3d66 + 0111237 commit e7ed948

File tree

1 file changed

+24
-14
lines changed
  • android/src/main/java/com/google/android/cameraview

1 file changed

+24
-14
lines changed

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

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -937,24 +937,34 @@ public Size getPreviewSize() {
937937
*/
938938
private void chooseCamera() {
939939
if(_mCameraId == null){
940-
int count = Camera.getNumberOfCameras();
941-
if(count == 0){
942-
//throw new RuntimeException("No camera available.");
943-
mCameraId = INVALID_CAMERA_ID;
944-
Log.w("CAMERA_1::", "getNumberOfCameras returned 0. No camera available.");
945-
return;
946-
}
947940

948-
for (int i = 0; i < count; i++) {
949-
Camera.getCameraInfo(i, mCameraInfo);
950-
if (mCameraInfo.facing == mFacing) {
951-
mCameraId = i;
941+
try{
942+
int count = Camera.getNumberOfCameras();
943+
if(count == 0){
944+
//throw new RuntimeException("No camera available.");
945+
mCameraId = INVALID_CAMERA_ID;
946+
Log.w("CAMERA_1::", "getNumberOfCameras returned 0. No camera available.");
952947
return;
953948
}
949+
950+
for (int i = 0; i < count; i++) {
951+
Camera.getCameraInfo(i, mCameraInfo);
952+
if (mCameraInfo.facing == mFacing) {
953+
mCameraId = i;
954+
return;
955+
}
956+
}
957+
// no camera found, set the one we have
958+
mCameraId = 0;
959+
Camera.getCameraInfo(mCameraId, mCameraInfo);
960+
}
961+
// getCameraInfo may fail if hardware is unavailable
962+
// and crash the whole app. Return INVALID_CAMERA_ID
963+
// which will in turn fire a mount error event
964+
catch(Exception e){
965+
Log.e("CAMERA_1::", "chooseCamera failed.", e);
966+
mCameraId = INVALID_CAMERA_ID;
954967
}
955-
// no camera found, set the one we have
956-
mCameraId = 0;
957-
Camera.getCameraInfo(mCameraId, mCameraInfo);
958968
}
959969
else{
960970
try{

0 commit comments

Comments
 (0)