0

I have been trying to figure out how to properly setup the android permissions for CAMERA and ACCESS_FINE_LOCATION on API 23+.

My program will request the CAMERA, but it will not request the ACCESS_FINE_LOCATION permission during runtime.

I have read permissions and location developers page, but I cannot seem to grasp how to request the permissions properly. I have also looked through stackoverflow for answers, but still do not understand.

Would someone be able to explain what I am doing wrong in my code, and how to fix it?

My code:

    private void connectCamera() {
    CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
    try {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.CAMERA) ==
                    PackageManager.PERMISSION_GRANTED) {
                cameraManager.openCamera(mCameraId, mCameraDeviceStateCallback, mBackgroundHandler);
            } else {
                if (shouldShowRequestPermissionRationale(android.Manifest.permission.CAMERA))  {
                    Toast.makeText(this,
                            "Video app required access to camera and location", Toast.LENGTH_SHORT).show();
                }
                requestPermissions(new String[]{android.Manifest.permission.CAMERA,
                        android.Manifest.permission.ACCESS_FINE_LOCATION,
                        Manifest.permission.ACCESS_COARSE_LOCATION
                }, REQUEST_CAMERA_PERMISSION_RESULT);
            }

        } else {
            cameraManager.openCamera(mCameraId, mCameraDeviceStateCallback, mBackgroundHandler);
        }
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}


    @Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == REQUEST_CAMERA_PERMISSION_RESULT) {
        if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
            Toast.makeText(getApplicationContext(),
                    "Application will not run without camera and location services!", Toast.LENGTH_SHORT).show();
        }
    }
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
goofenhour
  • 159
  • 1
  • 2
  • 13

1 Answers1

2

you're not asking for the permissions anywhere in the code you provided. To ask for the location feature, you can try the example below:

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

Also, you're not making your app open the camera once the user gives the permission to use this feature.

Inside your onRequestPermissionsResult() method, you should add an else calling openCamera.

Something like this:

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == REQUEST_CAMERA_PERMISSION_RESULT) {
        if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
            Toast.makeText(getApplicationContext(),
                    "Application will not run without camera and location services!", Toast.LENGTH_SHORT).show();
        }
        else {
            cameraManager.openCamera(mCameraId, mCameraDeviceStateCallback, mBackgroundHandler);
        }
    }
}

The verification steps are roughly something like the following:

  1. When the app opens, check if API >= 23
  2. If it's API >= 23, check if the permission is already granted.
  3. If it isn't, ask for the permission.
  4. The result from the request will be on the onRequestPermissionsResult() method. You have to handle the user input there. If he didn't gave permission, and this feature is essential for your app to run, show a dialog, or something else explaining why he should give you the permission, and ask again.
  5. If the permission was given, profit. Call the method on your app that will use that feature.

Take a look at this answer. That guy created a nice helper method to check and ask for any number of permissions you might need. Then, you'll just have to handle them on the onRequestPermissionsResult().

P.S.: I strongly recommend that you re-read the docs on how to request permissions on API 23+, your question shows that you didn't understand the basic concepts of permission requesting. Also, check this link of the Android training page, and this related question.

Mauker
  • 11,237
  • 7
  • 58
  • 76
  • Thank you. Do you have any recommendations about setting up my location permission as well? – goofenhour Oct 24 '17 at 16:59
  • It's the same workflow. Once you need to use the location features, you check for that permission, and ask the user if you don't have it. – Mauker Oct 24 '17 at 17:02
  • Also, I may add that GPS location is a feature that you MUST ask for permission if you're going to use it with API 23+ – Mauker Oct 24 '17 at 17:13
  • You mention that I was not asking for location permissions. Could you tell me what is going on in this code? I looks like to me that the below code will request CAMERA, FINE_LOCATION AND COARSE_LOCATION – goofenhour Oct 24 '17 at 22:01
  • requestPermissions(new String[]{android.Manifest.permission.CAMERA, android.Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION }, REQUEST_CAMERA_PERMISSION_RESULT); – goofenhour Oct 24 '17 at 22:02
  • You should take a look at this answer: https://stackoverflow.com/a/34343101/4070469 – Mauker Oct 25 '17 at 13:23