7

I'm trying run flashlight on API 21 and 22 but not working

Code

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
                isFlashlightOn();
                if (camera == null && parameters == null) {
                    camera = Camera.open();
                    parameters = camera.getParameters();
                    parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                    camera.setParameters(parameters);
                }
                if (getFlashlightState) {
                    Objects.requireNonNull(camera).startPreview();
                } else {
                    Objects.requireNonNull(camera).stopPreview();
                }
            } else {
                isFlashlightOn();
                if (cameraManager == null) {
                    cameraManager = (CameraManager) this.getSystemService(Context.CAMERA_SERVICE);
                }
                try {
                    String cameraId = Objects.requireNonNull(cameraManager).getCameraIdList()[0];
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                        cameraManager.setTorchMode(cameraId, getFlashlightState);
                    }else{
                        //the problem is here because I don't know what can I put in else
                    }
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }
            }

What is the alternative for setTorchMode in API 21 and 22

Taha Sami
  • 1,565
  • 1
  • 16
  • 43

1 Answers1

7

The function setTorchMode of new API Camera2 is available only since API 23. Old camera API should be used for flash in 21 and 22 API.

You also have to handle all flash modes available because some devices don't have FLASH_MODE_TORCH, but are compatible with FLASH_MODE_ON

You can change your code like this:

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
    isFlashlightOn();
    if (camera == null && parameters == null) {
        camera = Camera.open();
        parameters = camera.getParameters();

        List<String> modes = parameters.getSupportedFlashModes();
        if (modes.contains(Camera.Parameters.FLASH_MODE_TORCH)) {
            parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
        }
        else if (modes.contains(Camera.Parameters.FLASH_MODE_ON)) {
            parameters.setFlashMode(Camera.Parameters.FLASH_MODE_ON);
        }
        else {
            //No flash available
        }
        camera.setParameters(parameters);
    }
    if (getFlashlightState) {
        Objects.requireNonNull(camera).startPreview();
    } else {
        Objects.requireNonNull(camera).stopPreview();
    }
} else {
    isFlashlightOn();
    if (cameraManager == null) {
        cameraManager = (CameraManager) this.getSystemService(Context.CAMERA_SERVICE);
    }
    try {
        String cameraId = Objects.requireNonNull(cameraManager).getCameraIdList()[0];
        cameraManager.setTorchMode(cameraId, getFlashlightState);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}
dgp
  • 683
  • 3
  • 13
  • While largely correct, this code does not set a preview output target on the legacy camera API. That means preview will not actually start running on all devices, and the flash won't turn on, since only some devices allow preview to start without a preview target set. – Eddy Talvala Jan 19 '20 at 02:48
  • It will work just if torch is the only thing you want. In case flash needs to turn on/off during preview/capture, consider updating your `CaptureRequest.Builder` with subsequent call to `mCaptureSession.setRepeatingRequest` as it's demonstrated here: https://github.com/google/cameraview/blob/68947cc1643e7434250e099f38f346eae9c46339/library/src/main/api21/com/google/android/cameraview/Camera2.java#L544 – Cool Soft Jul 01 '22 at 10:35