Search code examples
androidandroid-cameraandroid-camera2flashlightandroid-compatibility

Controlling Flashlight in All Android devices


I'm using Camera Package for developing android app to turn on flash light, like this:

    Camera mCamera = Camera.open();
    List<String> flashModes = mCamera.getParameters().getSupportedFlashModes();
    if(flashModes != null && flashModes.contains(Parameters.FLASH_MODE_TORCH)){
        parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
        mCamera.setParameters(parameters);
    } else if (flashModes != null && flashModes.contains(Parameters.FLASH_MODE_ON)){
        parameters.setFlashMode(Parameters.FLASH_MODE_ON);
        mCamera.setParameters(parameters);
    }
    if(!previewing){
        mCamera.startPreview();
        previewing = true;
    }

It check supporting camera flash modes, and adjust it. It works in Samsung Android devices, and some others, but doesn't works in LG V20, LG V30 and Mi Android. I searched for this and tried:

mCamera.autoFocus(Camera.AutoFocusCallback)
mCamera.setPreviewTexture(SurfaceTexture)

But nothing works. So I used Camera2 package for api>23, like:

CameraManager mCameraManager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
mCameraManager.setTorchMode("0", true);

And it works, but in Samsung device, an notification show up "Flashlight turned on" and it's very annoying.

So, I need the way to turn on flashlight for all devices with Camera package, or not to show up "Flashlight turned on" notification using Camera2 package.

I look forward to your reply, thanks.


Solution

  • The answer in below link saves my life!

    The SurfaceView is essential for some devices.

    LED flashlight on Galaxy Nexus controllable by what API?