Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the alternative for setTorchMode in API 21 and 22

Tags:

android

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

like image 672
Taha Sami Avatar asked Jan 12 '20 11:01

Taha Sami


1 Answers

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();
    }
}
like image 178
dgp Avatar answered Nov 19 '22 05:11

dgp