The camera class is now out of date.
For LOLLIPOP above you need to use camera2 Api
so nickkadrov doesent solution works for 6.0 and above devices, the best way to turn on / off the flash is to try the code below
public static void toggleFlashLight(){ toggle=!toggle; try { CameraManager cameraManager = (CameraManager) getApplicationContext().getSystemService(Context.CAMERA_SERVICE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { for (String id : cameraManager.getCameraIdList()) { // Turn on the flash if camera has one if (cameraManager.getCameraCharacteristics(id).get(CameraCharacteristics.FLASH_INFO_AVAILABLE)) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { cameraManager.setTorchMode(id, true); } } } } } catch (Exception e2) { Toast.makeText(getApplicationContext(), "Torch Failed: " + e2.getMessage(), Toast.LENGTH_SHORT).show(); } }
where toggle is a static boolean of the class whose default value is false
static boolean toggle=false;
source share