Search code examples
androidandroid-camerax

how to avoid refreshing of screen while changing flash mode in android X


I'm trying to change the flash mode like below

when {
    ImageCapture.FLASH_MODE_AUTO == imageCapture!!.flashMode -> {
        imageCaptureFlashMode = ImageCapture.FLASH_MODE_ON
        idFlashControl.setImageResource(R.drawable.ic_flash_on)
        startCamera()
    }
...
...

in startCamera() I'm binding the use cases like below

cameraProvider?.unbindAll()
val cameraX = cameraProvider?.bindToLifecycle(
            this, cameraSelector, preview, imageCapture
        )

unbinding is recreating the surface. So, when I change the flash mode the screen goes black and come back to life like a new activity is created.

How to avoid this ?


Solution

  • You don't have to unbind/rebind the use cases when changing the image capture use case's flash mode. When you set the flash mode to FLASH_MODE_ON, the subsequent capture requests will use the flash (assuming the device supports flash).

    // Assuming the image capture has already been bound
    imageCapture.flashMode = ImageCapture.FLASH_MODE_ON
    
    // Assuming the flash unit is available, this capture request will use the flash
    imageCapture.takePicture(...)