如何使用cameraManager.setTorchMode(cameraId,false);在API21中

时间:2018-03-07 15:51:18

标签: android android-camera android-camera2

这是代码:

    public void turnOnFlashLight() {
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                cameraManager.setTorchMode(cameraId, true);
                playOnOffSound();
                powerButton.setImageResource(R.drawable.b3);
            }
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(Flash_main.this,"onFunction",Toast.LENGTH_SHORT).show();
        }
    }

    public void turnOffFlashLight() {

        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                cameraManager.setTorchMode(cameraId, false);
                playOnOffSound();
                powerButton.setImageResource(R.drawable.b4);

            }

        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(Flash_main.this,"offFunction",Toast.LENGTH_SHORT).show();
        }
    }

我正在运行android studio

请帮助,我想为Android 5.0及以上版本制作此应用程序,但

cameraManager.setTorchMode(cameraId, true);

但此库支持Android 6.0及更高版本

任何替代方案!

1 个答案:

答案 0 :(得分:0)

这是用于打开闪光灯的。

    if (is_flash_on) {

                if (mCamera == null || p == null) {
                    return;
                }

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    try {
                        String cameraId;
                        camManager = (CameraManager) this.getSystemService(Context.CAMERA_SERVICE);
                        if (camManager != null) {
                            cameraId = camManager.getCameraIdList()[0];
                            camManager.setTorchMode(cameraId, false);
                        }
                    } catch (CameraAccessException e) {
                        e.printStackTrace();
                        Toast.makeText(getApplicationContext(), e.getMessage() , Toast.LENGTH_SHORT).show();

                    }
                } else {
                    p = mCamera.getParameters();
                    p.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
                    mCamera.setParameters(p);
                    mCamera.stopPreview();

                    is_flash_on = false;
                    torchImageView.setImageResource(R.drawable.ic_lightbulb_outline_black_24dp);
                }

            }

这用于关闭较旧的api。

       if (!is_flash_on) {
            if (mCamera == null || p == null) {
                return;
            }

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                try {
                    camManager = (CameraManager) this.getSystemService(Context.CAMERA_SERVICE);
                    String cameraId = null; // Usually front camera is at 0 position.
                    if (camManager != null) {

                        cameraId = camManager.getCameraIdList()[0];
                        camManager.setTorchMode(cameraId, true);
                    }
                } catch (CameraAccessException e) {
                    Log.e("Camera", e.toString());

                    torchImageView.setImageResource(R.drawable.ic_lightbulb_outline_yellow_64dp);
                }
            } else {
                p = mCamera.getParameters();
                p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                mCamera.setParameters(p);
                mCamera.startPreview();
                is_flash_on = true;
                torchImageView.setImageResource(R.drawable.ic_lightbulb_outline_yellow_64dp);
            }

        }