How to turn on front flash light programmatically in Android?

后端 未结 11 1614
执笔经年
执笔经年 2020-11-22 00:17

I want to turn on front flash light (not with camera preview) programmatically in Android. I googled for it but the help i found referred me to this page

Does anyo

11条回答
  •  自闭症患者
    2020-11-22 01:02

    In Marshmallow and above, CameraManager's `setTorchMode()' seems to be the answer. This works for me:

     final CameraManager mCameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
     CameraManager.TorchCallback torchCallback = new CameraManager.TorchCallback() {
         @Override
         public void onTorchModeUnavailable(String cameraId) {
             super.onTorchModeUnavailable(cameraId);
         }
    
         @Override
         public void onTorchModeChanged(String cameraId, boolean enabled) {
             super.onTorchModeChanged(cameraId, enabled);
             boolean currentTorchState = enabled;
             try {
                 mCameraManager.setTorchMode(cameraId, !currentTorchState);
             } catch (CameraAccessException e){}
    
    
    
         }
     };
    
     mCameraManager.registerTorchCallback(torchCallback, null);//fires onTorchModeChanged upon register
     mCameraManager.unregisterTorchCallback(torchCallback);
    

提交回复
热议问题