From 12e28c462f1f114499d6a615020fccdaa84ecd14 Mon Sep 17 00:00:00 2001 From: Amir Abdi Date: Mon, 25 Sep 2017 12:06:39 +0330 Subject: [PATCH] Fix issue #12 --- .../torchlight2/camera/CameraMarshmallow.java | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/com/secuso/torchlight2/camera/CameraMarshmallow.java b/app/src/main/java/com/secuso/torchlight2/camera/CameraMarshmallow.java index 90c0131..c5766b7 100644 --- a/app/src/main/java/com/secuso/torchlight2/camera/CameraMarshmallow.java +++ b/app/src/main/java/com/secuso/torchlight2/camera/CameraMarshmallow.java @@ -22,10 +22,17 @@ public class CameraMarshmallow implements ICamera { public void init(Context context) { mContext = context; mCameraManager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE); - + try { - final String[] list = mCameraManager.getCameraIdList(); - mCameraID = list[0]; + for(final String cameraId : mCameraManager.getCameraIdList()){ + CameraCharacteristics characteristics = mCameraManager.getCameraCharacteristics(cameraId); + int cOrientation = characteristics.get(CameraCharacteristics.LENS_FACING); + if(cOrientation == CameraCharacteristics.LENS_FACING_BACK){ + mCameraID= cameraId; + } + } + + } catch (CameraAccessException e) { Toast.makeText(mContext, e.getMessage(), Toast.LENGTH_SHORT).show(); } @@ -36,8 +43,12 @@ public void init(Context context) { @TargetApi(Build.VERSION_CODES.M) public boolean toggle(boolean enable) { try { - mCameraManager.setTorchMode(mCameraID, enable); - return true; + if (mCameraID != null){ + mCameraManager.setTorchMode(mCameraID, enable); + return true; + }else{ + return false; + } } catch (CameraAccessException e) { Toast.makeText(mContext, e.getMessage(), Toast.LENGTH_SHORT).show(); return false;