Search code examples
javaandroidcameraandroid-cameraandroid-camera-intent

Why app doesn't see permission Camera2API?


I have a really big issue(

I'm working with Camera2API and and I use for it standard Google example.

Mystic is when I use this method

private void openCamera(int width, int height) {
    setUpCameraOutputs(width, height);

    configureTransform(width, height);
    CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
    try {
        if (!mCameraOpenCloseLock.tryAcquire(2500, TimeUnit.MILLISECONDS)) {
            throw new RuntimeException("Time out waiting to lock camera opening.");

        }

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) !=
                PackageManager.PERMISSION_GRANTED) {
            return;
        }

        manager.openCamera(mCameraId, mStateCallback, mBackgroundHandler);


    } catch (CameraAccessException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        throw new RuntimeException("Interrupted while trying to lock camera opening.", e);
    }
}

I use Samsung S6 android 6.0

I expect that method manager.openCamera(mCameraId, mStateCallback, mBackgroundHandler); - will open the camera, but instead of this, code is stoping in

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) !=
                PackageManager.PERMISSION_GRANTED) {
            return;
        }

as far as I understand, it means that my manifest does not contain permission, but I checked and there is an appropriate permission

<uses-permission android:name="android.permission.CAMERA" />

What does it mean?

I have this permission, why it doesn't pass checking?

I tried launching this app on Meizu MX5 android 5.1 and it opens the camera and everything is ok...

Maybe the problem is in androind version?

What I am doing wrong? How can I check it?


Solution

  • You said you're using Android 6.0 right?
    According to this even if you declare the permissions in the manifest, you have to individually check for each permission, request each permission if it hasn't been granted.
    To do this, after :

    if (ActivityCompat.checkSelfPermission(this,Manifest.permission.CAMERA) !=
            PackageManager.PERMISSION_GRANTED) {
            return;
    }
    

    Instead of return , do this ,

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

    and implement the callback function as described in the linked page,

    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           String permissions[], int[] grantResults) {
        switch (requestCode) {
            case YOUR_INTEGER_CONSTANT_FOR_THE_CALLBACK: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    Log.d(TAG,"Granted");
                    // permission was granted, yay! Do the
                    // contacts-related task you need to do.
    
                } else {
                    Log.d(TAG,"Denied");
                    // permission denied, boo! Disable the
                    // functionality that depends on this permission.
                }
                return;
            }
    
            // other 'case' lines to check for other
            // permissions this app might request
        }
    }
    

    Hope that helps.