Opening flashlight in android with various modes

Will the code below work on Android devices such as a Motorola razor that does not support the torch? Can someone please be kind enough to check if you have such a phone and please tell me.

Thanks in advance!

       if (!isFlashOn) {
           if (camera == null || params == null) {
               return;
             }     
           List<String> flashModes = params.getSupportedFlashModes();
           if(flashModes.contains(Parameters.FLASH_MODE_TORCH)){

          try { 
                   params = camera.getParameters();
                   params.setFlashMode(Parameters.FLASH_MODE_TORCH);
                   camera.setParameters(params);
                   camera.startPreview();
                   toggleButtonImage();
                   isFlashOn = true;

           }catch (RuntimeException e) {
            }

           }

           else {
                    params = camera.getParameters();
                    params.setFlashMode(Parameters.FLASH_MODE_ON);
                    camera.setParameters(params);
                    camera.startPreview();
                    toggleButtonImage();
                    isFlashOn = true;
           }
           toggleButtonImage();
           isFlashOn = true;

       }

   }

PS Should I add something like:

if (flashModes.contains(android.hardware.Camera.Parameters.FLASH_MODE_AUTO))
           {
                params.setFlashMode(Parameters.FLASH_MODE_AUTO);
                camera.setParameters(params);
                camera.startPreview();
           }

It works on Motorola g and Galaxy S4 (torch supported)

+4
source share
3 answers

You can open the flashlight in different modes if different flash modes are supported on your device. You can get the code from this open source code. OPenCamera

+6

, , . , , Android. .

+2

-, , setFlashMode ,

public List<String> getSupportedFlashModes() {
    return params.getSupportedFlashModes();
}

:

Camera.Parameters.FLASH_MODE_AUTO, Camera.Parameters.FLASH_MODE_OFF, Camera.Parameters.FLASH_MODE_ON, Camera.Parameters.FLASH_MODE_RED_EYE, Camera.Parameters.FLASH_MODE_TORCH

But some or any of these flash modes may not be available on your device, check before use. After choosing from flash modes, you can set flash modes using this method.

public synchronized void setFlashMode(String flashMode) {
        Camera.Parameters params = mCamera.getParameters();
        if (cameraId == Camera.CameraInfo.CAMERA_FACING_BACK && params.getSupportedFlashModes() != null
            && params.getSupportedFlashModes().contains(flashMode)) {
        params.setFlashMode(flashMode);
        mCamera.setParameters(params);
    }
}
0
source

Source: https://habr.com/ru/post/1526137/


All Articles