简体   繁体   中英

How to check, on Android, if device has flash light led

How can I check if a device has a camera led (flashlight)? I am talking about devices with android OS?

I have seen solutions some solutions which talks about how to turn the led on and off but what will happen if the device doesn't even has a led.

for turning on the camera I am using camera.open()

The other answers

boolean hasFlash = this.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);

Does not work for the new 2013 Nexus 7. The following code will work:

public boolean hasFlash() {
        if (camera == null) {
            return false;
        }

        Camera.Parameters parameters;
        try {
            parameters = camera.getParameters();
        } catch (RuntimeException ignored)  {
            return false;
        }

        if (parameters.getFlashMode() == null) {
            return false;
        }

        List<String> supportedFlashModes = parameters.getSupportedFlashModes();
        if (supportedFlashModes == null || supportedFlashModes.isEmpty() || supportedFlashModes.size() == 1 && supportedFlashModes.get(0).equals(Camera.Parameters.FLASH_MODE_OFF)) {
            return false;
        }

        return true;
    }

getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH) returns true if the device has flash. See this for more details

You should be able to check whether the flash is available by checking system features:

boolean hasFlash = this.getPackageManager()
                       .hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);

(provided you are in an Activity ). If not, than use some sort of context in place of this .

PS Note that this information is quite easy to find if you actually try searching for it.

PackageManager pm = context.getPackageManager();
        if (!pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
            Log.e("err", "Device has no camera!");
            return;
        }       
        camera = Camera.open();
        p = camera.getParameters();
        flashModes = p.getSupportedFlashModes();
if(flashModes==null){
                        Toast.makeText(getApplicationContext(), "LED Not Available",Toast.LENGTH_LONG).show();
                }else
                {
Toast.makeText(getApplicationContext(), "LED  Available",Toast.LENGTH_LONG).show();
}

This is how I check if LED flash is available. Also you don't need to have the Camera permission to run this method.

private fun isLedFlashAvailable(context: Context): Boolean {
    // method 1
    if (context.packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)) {
        return true
    }

    // method 2
    val cameraManager = context.getSystemService(Context.CAMERA_SERVICE) as CameraManager
    for (id in cameraManager.cameraIdList) {
        if (cameraManager.getCameraCharacteristics(id).get(CameraCharacteristics.FLASH_INFO_AVAILABLE) == true) {
            return true
        }
    }

    return false
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM