How to turn on FlashLight in Lollipop programmatically Android

后端 未结 3 801
天涯浪人
天涯浪人 2021-02-06 01:51
Camera cam = Camera.open();     
Parameters p = cam.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
cam.setParameters(p);
cam.startPreview();
3条回答
  •  醉酒成梦
    2021-02-06 02:32

    Your code should actually work. Please check if you added the permission for using the camera properly:

    
    
    

    This should be added to your AndroidManifest above of your other specifications.

    Additionally, there is an interesting discussion about different devices and an example which should work on every device here: Flashlight in Android

    If you dont want to use the deprecated API, you can check out:

    Package Summary of Camera2

    Camera device specification on the new api

    Unfortunately I can not give you an example for using the new API, because I did not use it myself yet.

提交回复
热议问题