Search code examples
androidandroid-cameraxcamera-flash

FLASH_MODE_TORCH in CameraX


I am trying to keep the flash on all time by setting flash to FLASH_MODE_TORCH in CameraX in my app. But, I could not find this flash mode for cameraX. Is this mode not available in cameraX? If not, is there any alternative to keep my flash on whole time?


Solution

  • You should be able to enable the torch by calling CameraControl.enableTorch(true), it internally sets the capture request's flash mode to FLASH_MODE_TORCH.

    If the camera does not have a flash unit (i.e. CameraInfo.hasFlashUnit() returns false), calling enableTorch(true) will be no-op.

    final Camera camera = cameraProvider.bindToLifecycle(...);
    final CameraInfo cameraInfo = camera.getCameraInfo();
    final CameraControl cameraControl = camera.getCameraControl();
    
    if (cameraInfo.hasFlashUnit()) {
        cameraControl.enableTorch(true); // You can listen to this future to handle errors
    }