title: Turn On flashlight when camera is running
body: <p>I would like to turn on the flashlight when the Camera is running but i am getting the following exception:</p>
  
  <pre><code>W/System.err: android.hardware.camera2.CameraAccessException: CAMERA_IN_USE (4): setTorchMode:1611: Torch for camera "0" is not available due to an existing camera user
  </code></pre>
  
  <p>I am able to turn on the turn on the flashlight when the camera is not running, but when the camera is starting the flashlight is turn off and when i am pressing the button in my app to turn it on I am getting to above exception.</p>
  
  <p>when I am using the flashlight system application, the flashlight is turn on also when the camera is running.</p>
  
  <p>i was able to create a service to turn on the flashlight and it the service is working, but again, when the camera is running, i am getting the above exception.</p>
  
  <p>here is my Service Class:</p>
  
  <pre><code>public class FlashLightService extends Service {
      private CameraManager camManager;
      private String cameraId;
  
      @Override
      public IBinder onBind(Intent intent) {
          return null;
      }
  
      @Override
      public void onCreate() {
          camManager = (CameraManager) 
      getSystemService(Context.CAMERA_SERVICE);
          try {
              cameraId = camManager.getCameraIdList()[0];
              Log.i("CAMERA HAS FLASH:",
                  "" + camManager.getCameraCharacteristics(cameraId).get(CameraCharacteristics.FLASH_INFO_AVAILABLE));
          } catch (CameraAccessException e) {
              e.printStackTrace();
          }
      Toast.makeText(this, "Service Created", Toast.LENGTH_LONG).show();
      }
  
      @Override
      public int onStartCommand(Intent intent, int flags, int startId) {
          try {
              Log.i("CAMERA TURN ON", "cameraId=" + cameraId);
              camManager.setTorchMode(cameraId, true);
          } catch (CameraAccessException e) {
              e.printStackTrace();
          }
          return super.onStartCommand(intent, flags, startId);
      }
  
      @Override
      public void onDestroy() {
          try {
              Log.i("CAMERA TURN OFF", "cameraId=" + cameraId);
              camManager.setTorchMode(cameraId, false);
          } catch (CameraAccessException e) {
              e.printStackTrace();
          }
      }
  } 
  </code></pre>
  
  <p>here is the relevant code at the activity: </p>
  
  <pre><code>toggleFlashlightButton.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
          if (isFlashOn) {
              setFlashOff();
          } else {
              setFlashOn();
          }
      }
  });
  
  private void setFlashOn() {
      isFlashOn = true;
      toggleFlashlightButton.setImageResource(R.drawable.flashlight_off);
      startService(flashLightService);
  }
  
  private void setFlashOff() {
      isFlashOn = false;
      toggleFlashlightButton.setImageResource(R.drawable.flashlight_on);
      stopService(flashLightService);
  }
  </code></pre>
  
  <p>I am actually want to be able to activate the flashlight together with the camera from my application.</p>