2

I've tried countless times to get a flashlight to work with a tablet that I'm working with. It is supposed to be very simple testing-app to test the tablet in question, so nothing fancy.

I have tried using CameraManager, I've tried using legacy Camera-class, everything, but no matter what I do I keep getting the same error: "Torch for camera "0" is not available due to an existing camera user".. This keeps happening even if I disable the camera App. The tablet that I'm using is running Android 8.1 and is a fresh install.

Here's the OnCreate function of this Activity:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_flashlight);


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



      tryInitCamera();

    }

Here's the tryInitCamera, which gives successfully the camera Id as 0 and identifies, that the flash is available:

private void tryInitCamera() {
        try {
            String[] ids = mcameramanager.getCameraIdList();
            for (String id : ids) {
                CameraCharacteristics c = mcameramanager.getCameraCharacteristics(id);
                Boolean flashAvailable = c.get(CameraCharacteristics.FLASH_INFO_AVAILABLE);
                Integer lensFacing = c.get(CameraCharacteristics.LENS_FACING);
                if (flashAvailable != null && flashAvailable
                        && lensFacing != null && lensFacing == CameraCharacteristics.LENS_FACING_BACK) {
                    mcameraID = id;
                    return;
                }
            }
        } catch (Throwable e) {
            Log.println(Log.ERROR, "CAMERA", "INITIALIZATION FAILED");
            return;
        }
    }

And here's the part where the flashlight is being turned on and off by a button elsewhere. I've checked that this code is being executed:

public void setFlashlight(boolean enabled) {


        try {

            if (mcameraID == null) {
                while (mDoInit) {
                    Thread.sleep(1000);
                    tryInitCamera();

                    if (mcameraID != null) {
                        break;
                    }
                }
            }
            if (mcameraID != null)
                try {

                    mcameramanager.setTorchMode(mcameraID, enabled);

                }catch (Exception e){
                    e.printStackTrace();
                }

        } catch (Exception e) {
            e.printStackTrace();
        }

        flashlight_status = (!flashlight_status);

    }

Permissions are being handled like this:

private void requestPermission() {

        ActivityCompat.requestPermissions(this,
                new String[]{Manifest.permission.CAMERA},
                PERMISSION_REQUEST_CODE);
    }



    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case PERMISSION_REQUEST_CODE:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    Toast.makeText(getApplicationContext(), "Permission Granted", Toast.LENGTH_SHORT).show();

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

                    try {
                        String cameraId = cameraManager.getCameraIdList()[0];
                        cameraManager.setTorchMode(cameraId, true);
                    }catch(CameraAccessException e){

                    }
                } else {
                    Toast.makeText(getApplicationContext(), "Permission Denied", Toast.LENGTH_SHORT).show();
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                        if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
                                != PackageManager.PERMISSION_GRANTED) {
                            showMessageOKCancel("You need to allow access permissions",
                                    new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                                                requestPermission();
                                            }
                                        }
                                    });
                        }
                    }
                }
                break;
        }
    }



    private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener) {
        new AlertDialog.Builder(FlashlightActivity.this)
                .setMessage(message)
                .setPositiveButton("OK", okListener)
                .setNegativeButton("Cancel", null)
                .create()
                .show();
    }

Here's all the permissions that I have at the manifest:


<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.NFC" />

<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.any" />
<uses-feature android:name="android.hardware.camera.flash" />
<uses-feature android:name="android.hardware.camera2.full" />

Every time, it fails with: mcameramanager.setTorchMode(mcameraID, enabled);

I have permissions right, and I don't get any warnings regarding permissions or anything. What on earth can I do? I have tried at least two different methods, I have tried to Camera.open() -method, but it keeps telling me, that the camera is not available. Android's own flashlight-toggle works fine though.

Edit. Oh, and if I use the built in camera-app, everything works fine, even the flash.

Edit 2, I'm not trying to use the camera at the same time, so all this activity is trying to do, is to toggle flashlight on and off, no camera preview needed. The camera is being tested elsewhere.

DeadlyHigh
  • 94
  • 8

0 Answers0