在android6.0中,加入了Camera相关新特性,在做开发过程中,遇到闪光灯的操作,如下:

   1.导入包: import android.hardware.camera2.CameraManager;

        import android.hardware.camera2.CameraAccessException;

   2.在onCreate函数中初始化CameraManager:

        camerager = (CameraManager)getSystemService(Context.CAMERA_SERVICE);

   3.函数:

        //flag: true,亮灯 false:灭灯

        private void openOrCloseFlashlight(boolean flag){

            try{

                if(camerager != null){

                    camerager.setTorchMode("0", flag);//0,指cameraId,通常0代表后置,1代表前置

                }

            }catch(CameraAccessException ex){

                ex.printStackTrace();

            }

       }