0

This is the code :

    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();
        }
    }

I am running android studio

please help, I want to make this app for android 5.0 and above but

cameraManager.setTorchMode(cameraId, true);

but this library supports Android 6.0 and above

Any Alternative Please!

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

1 Answers1

1

This is for turning on flash.

    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);
                }

            }

This is for turning off for older 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);
            }

        }
JustABeginner
  • 785
  • 2
  • 11
  • 26
  • 1
    If I used `setTorchMode(cameraId, true)`, then the torch turned off when the camera was configured. If used after configuration, I got a `CAMERA_IN_USE` exception. This link shows how to configure the flash in the `CaptureRequest.Builder`: – BlueSpectrumz Mar 25 '20 at 16:36