how to check if device has flash light led android

前端 未结 5 914
旧巷少年郎
旧巷少年郎 2020-12-10 01:36

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

5条回答
  •  余生分开走
    2020-12-10 01:39

    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 supportedFlashModes = parameters.getSupportedFlashModes();
            if (supportedFlashModes == null || supportedFlashModes.isEmpty() || supportedFlashModes.size() == 1 && supportedFlashModes.get(0).equals(Camera.Parameters.FLASH_MODE_OFF)) {
                return false;
            }
    
            return true;
        }
    

提交回复
热议问题