Android: how to check the flash light is available on device?

前端 未结 4 485
一生所求
一生所求 2021-01-18 10:27

How I check the flash light available on device?also want to know how can I On/Off the flash light? I have put the code but not working right now? I search out this

相关标签:
4条回答
  • 2021-01-18 10:39

    This can help full to turn on/off flash light of device. This is give me satisfaction, Hope be useful to you also.

    Turn On camera Flash

    camera = Camera.open();
    Parameters p = camera.getParameters();
    p.setFlashMode(Parameters.FLASH_MODE_TORCH);
    camera.setParameters(p);
    camera.startPreview();
    

    Turn Off camera Flash

    camera = Camera.open();
    Parameters p = camera.getParameters();
    p.setFlashMode(Parameters.FLASH_MODE_OFF);
    camera.setParameters(p);
    camera.stopPreview();
    

    Put this Permission in manifest file

    <uses-permission android:name="android.permission.CAMERA" />
    <uses-feature android:name="android.hardware.camera" />
    

    For more detail you can go HERE.

    0 讨论(0)
  • 2021-01-18 10:40

    First you get supported flash modes:

    camera = Camera.open(i);    // Introduced in API level 9
    parameters = camera.getParameters();
    String[] flashModes = parameters.getSupportedFlashModes();
    

    And then you check if this array contains the correct constants like: "auto", "on", "off".

    More info in: http://developer.android.com/reference/android/hardware/Camera.Parameters.html#FLASH_MODE_AUTO

    0 讨论(0)
  • 2021-01-18 10:53

    You can use the following

    context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
    

    which will return true if a flash is available, false if not.

    See http://developer.android.com/reference/android/content/pm/PackageManager.html for more information.

    0 讨论(0)
  • 2021-01-18 10:53
    boolean hasFlash =this.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
    

    or

    public boolean hasFlash() {
            if (camera == null) {
                return false;
            }
    
            Camera.Parameters parameters = camera.getParameters();
    
            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;
        }
    
    0 讨论(0)
提交回复
热议问题