I am trying to open flash light when camera is open but it’s working only if the camera is off, when back camera is on, it is throwing exception
Torch for camera “0” is not available due to an existing camera user
I have used below code snippet
public void flashLight(Context context, Boolean flashState) {
try {
CameraManager cameraM = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
for (String cameraID : cameraM.getCameraIdList()) {
CameraCharacteristics characterStics = cameraM.getCameraCharacteristics(cameraID);
if (characterStics.get(CameraCharacteristics.LENS_FACING) == CameraCharacteristics.LENS_FACING_BACK) {
try {
cameraM.setTorchMode(cameraID, flashState);
if (flashState)
Log.d("Flash", "Flash light is on");
else
Log.d("Flash", "Flash light is off");
} catch (CameraAccessException e) {
Log.d("connectorFlash", e.getMessage());
}
}
}
} catch (Exception exception) {
Log.d("Flash", exception.getMessage());
}
}