首先在清单文件添加权限
<!-- 控制闪光灯权限-->
<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-permission android:name="android.permission.CAMERA" />
然后通过以下代码控制闪光灯
@RequiresApi(api = Build.VERSION_CODES.M)
private void toggleFlashLight(boolean isEnable) {
CameraManager cameraManager =(CameraManager) getSystemService(Context.CAMERA_SERVICE);
try {
cameraManager.setTorchMode("0",isEnable);
}catch (CameraAccessException e) {
e.printStackTrace();
}
}```