Question

Will the code as below work on android devices like Motorola razor who doesn't support torch? Can someone please be kind enough to test if has any phone like that 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;

       }

   }

P.S Should i additionally 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)

Was it helpful?

Solution

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

OTHER TIPS

Yes, if You check that device supports torch. But You can encounter a Device-Specific issue that is very prevalent in the Android. You can find more information in this post.

you should check supported flash modes to not have an exception, setFlashMode method checks for supported modes but checking with this method is helpful to set flash mode button or view on UI

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

Sum of all Flash modes are:

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 in your device, check before using. After selecting from flash modes you can set the 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);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top