Search code examples
javaandroidflashlight

How to open device flashlight in Android N?


I'm trying to make a simple button that will turn on/off the device flashlight. I don't understand why android.hardware.camera is obsolete. What do I have to do in order to make my code working on all devices and also ones with older version of Android?

This is my code:

if (IsFlashlightOn)
{
  if (getPackageManager().hasSystemFeature(
        PackageManager.FEATURE_CAMERA_FLASH)) 
  {
    cam = Camera.open();
    Camera.Parameters p = cam.getParameters();
    p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
    cam.setParameters(p);
    cam.startPreview();
  }
  else
  {
    try
    {
      cam.stopPreview();
      cam.release();
      cam = null;
    }
    catch (Exception ex)
    {
      // Ignore the exception
    }
  }
}

Solution

  • There is a mistake in the logic of your code. It's not related to any specific Android version. You are checking if the device has camera flashlight and then, turns it on. In the else block you are turning the camera flashlight off in the case when a device has no camera flashlight what will never happen if your device actually has a flashlight.

    I think the code should look like below. It will toggle flashlight (turn it on, when it's turned off and turn it off when it's turned on).

    boolean isFlashlightOn = false;
    boolean deviceHasCameraFlash = getPackageManager().hasSystemFeature(
                                     PackageManager.FEATURE_CAMERA_FLASH);
    
    if(deviceHasCameraFlash) {
      Camera camera = Camera.open();
      Camera.Parameters parameters = camera.getParameters();
    
      if(isFlashlightOn) { 
        // turn the flashlight off
        parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
        camera.setParameters(parameters);
        camera.stopPreview();
        isFlashlightOn = false;
      } else {
        // turn the flashlight on
        parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
        camera.setParameters(parameters);
        camera.startPreview();
        isFlashlightOn = true;
      }
    }
    

    I couldn't test this code right now, but I think it should work, you should get the general idea now and adjust it to your purposes.

    To avoid warnings in the IDE and Static Code Analysis tools, you need to add @SuppressWarnings("deprecation") annotation to the deprecated code. We need to keep it in order to have backward compatibility with older Android versions.

    If you want to handle Camera on both new and old Android versions, you should prepare the separate code for these versions.

    According to the documentation:

    We recommend using the new android.hardware.camera2 API for new applications.

    It means you should do it in the following way:

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
      // code for lollipop devices or newer
    } else {
      // code for pre-lollipop devices
    }
    

    Code for toggling flashlight with the new API would look as follows:

    private void toggleFlashLight(boolean isFlashlightOn) {
      CameraManager camManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
      String cameraId = camManager.getCameraIdList()[0]; // Usually front camera is at 0 position.
      camManager.setTorchMode(cameraId, isFlashlightOn);
    }
    

    Moreover, check out these StackOverflow threads:

    They may be helpful while dealing with your issue.

    Regards