How to use both camera preview and flashlight?

I am facing an exception “Camera is already in use”. The thing i am doing is that i am using camera with seek bar as a magnifier and now i want to add flash functionality with it but i am facing this exception.

I am using this code for flash.

private void torchToggle(String command) {


        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                String cameraId = null; // Usually back camera is at 0 position.
                try {
                    if (camManager != null) {
                        cameraId = camManager.getCameraIdList()[0];
                    }
                    if (camManager != null) {
                        if (command.equals("on")) {
                            camManager.setTorchMode(cameraId, true);   // Turn ON

                            Toast.makeText(this, "Flash on", Toast.LENGTH_SHORT).show();
                        } else {
                            if (getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH) == true) {
                                camManager.setTorchMode(cameraId, false);  // Turn OFF
                                Toast.makeText(this, "Flash off", Toast.LENGTH_SHORT).show();

                            }

                        }
                    }
                } catch (CameraAccessException e) {
                    Toast.makeText(this, ""+e, Toast.LENGTH_SHORT).show();
                }
            }
            else
            {
                Toast.makeText(this, "device version too low", Toast.LENGTH_SHORT).show();
            }
        }
        catch (Exception e)
        {
            Toast.makeText(this, "Issue: "+ e, Toast.LENGTH_SHORT).show();
        }
    }

Leave a Reply

avatar
  Subscribe  
Notify of