Permission Denial... requires android.permission.READ_EXTERNAL_STORAGE or grantUriPermission()


#1

My Ionic app (in development) used to work fine with Android 5. When testing it with a new Google Nexus 6P (Android 6.0.1) I get the error message in the title. My camera code is simple (see below). What am I missing to make it work again?

angular.module('my.services').service('phoneUtilitiesService', ....

                this.takePicture = function( type, successFn, errFn ) {
                navigator.camera.getPicture( successFn, errFn, {
                    quality: 75,
                    allowEdit: false,
                    encodingType: navigator.camera.EncodingType.JPEG,
                    targetWidth: 512,
                    targetHeight: 512,
                    cameraDirection: type=='self'? 1:0 , // selfie==1, normal==0
                    saveToPhotoAlbum: false,
                    correctOrientation: true
                });
            };

#2

What has changed with android 6.0.1 from 5, is that instead of at install, it will now request permissions when the app attempts to use a feature.

For more information, see the following documentation:
https://developer.android.com/training/permissions/requesting.html

With a little research, I discovered a plugin as been developed at:

You should use this to check if permissions have been requested, and if so then execute your request. If they do not allow, you can simply show an alert or something telling the user that it is required.


#3

Hi EffkaT, thanks for the information.
Do you happen to know how to modify the manifest file (or elsehwre) to ask for permission to use camera in that form:

     `navigator.camera.getPicture( successFn, errFn, options)` ?

Also, would that change affect use on earlier Android versions, do we need to specify differently to work with old and new Androids??

Thanks much!
Z.


#4

I haven’t tested it, but you shouldn’t need to add anything new to the manifest, instead just install the plugin, and call

//only if v6 or greater
if (ionic.Platform.version() >= 6) {
  var permissions = window.plugins.permissions;
  //do we already have permissions
  permissions.hasPermission(function(status){
    if(!status.hasPermission) {
      //if not, warn in console
      var errorCallback = function() {
        console.warn('Camera permission is not turned on');
      }
      //make request for permissions
      permissions.requestPermission(function(status) {
        //do we still not have permissions? user denied. Do something here
        if( !status.hasPermission ) userdenied();
      }, function(){}, permissions.CAMERA);
    }
  }, function(){}, permissions.CAMERA);
}

What this would do is check the version of android, if it is above 6.0, it will request permission, otherwise it will ignore the check.


#5

Thanks much. I guess userdenied() was meant to be errorCallback() .

But the problem persists. When running it I get the error message that permission is not turned on. But there is no dialog popping up requesting such permission.

The Ionic app was installed using “ionic run android” . It did not ask for permissions setting when installed. How do we make ionic app display and request permissions when they are installed?

BTW when looking at the Settings / apps / my-under-dev-app it allows setting all kind of permissions such as “contacts”, “accounts”, location, etc, but not the Camera. Should specific libraries be used with the camera (vs the navigator.camera.getPicture I have been using in order to get the Camera permissions available?

Thanks again !


#6

got it. For whomever may face this issue, the following line needs to be added to
./platforms/android/AndroidManifest.xml

and the line is:
<uses-permission android:name="android.permission.CAMERA" />

And of course the code EffakT provided above needs to be added as well. Note that there is a typo there, instead of userdenied the correct call is to errorCallback .

Thanks EffakT !


#7

This will be overwritten when you do an ionic state reset. Which a build server does.
How did you fix this?


#8

@EffkT, i tried this it didnot work for me.
im using ionic 1.3.1 and angular 1.5.3.
the app just stops when i run it in the emulator and it crashes when i try to run it on a phone with android marshmallow (6.0)

Please help


#9

sallawcos, did u fix this ?