0

I have only minor experience with Android and have the following snippet:

String cameraId = cameraManager.getCameraIdList()[1]; System.out.println(cameraId); cameraManager.setTorchMode(cameraId, true);

which should turn on the flash on the backside camera (Ideally). This snippet is executed in an onClickListener. When the cameraManager.setTorchMode(cameraId, true); gets executed, the app gets minimized (I can see its still running - not crashed) but the flash is not turned on.

I am almost exhausted after experimenting options like adding extra check whether the flash is available and all. The project is set for Jellybean+ versions of Android. The test phone is Lollipop 5.0.2.

I know this question may be a duplicate of : How to open device flashlight in Android N?

But had exhaustively tried every option and desperately need a solution. Thanks in advance.

Ginu Jacob
  • 1,588
  • 2
  • 19
  • 35

1 Answers1

0
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        mCameraManager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
        try {
            for (String camID : mCameraManager.getCameraIdList()) {
                CameraCharacteristics cameraCharacteristics = mCameraManager.getCameraCharacteristics(camID);
                int lensFacing = cameraCharacteristics.get(CameraCharacteristics.LENS_FACING);
                if (lensFacing == CameraCharacteristics.LENS_FACING_BACK 
                        && cameraCharacteristics.get(CameraCharacteristics.FLASH_INFO_AVAILABLE)) {
                    mCameraId = camID;
                    break;
                }
            }
            if (mCameraId != null) {
                mCameraManager.setTorchMode(mCameraId, true);
            }
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
    }

mCameraId will turn on back camera flash. If device has no flash hardware then mCameraId will be null and setTorchMode will not be called in the code above.

LokiDroid
  • 972
  • 10
  • 22