Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cordova App 'Android permission Cordova plugin' is not showing permission dialog

i am attending to use 'imagepicker' plugin for my cordova app to get images from the mobile gallery and use them . i am testing my app on android 6.0 device and this is the problem, marshmallow Android 6.0 require in run-time permission not like the older versions "it is working on older versions" ,but on api 23 or higher when it attend to open gallery it close immediately and the app crashes . when i searched i found that i need permission to do it . so i started to use "Android permission Cordova plugin" and by copying the example they presented in this page : https://www.npmjs.com/package/cordova-plugin-android-permissions

which is :

var permissions = cordova.plugins.permissions;
permissions.hasPermission(permissions.CAMERA, checkPermissionCallback, null);

function checkPermissionCallback(status) {
  if(!status.hasPermission) {
    var errorCallback = function() {
      console.warn('Camera permission is not turned on');
    }

    permissions.requestPermission(
      permissions.CAMERA,
      function(status) {
        if(!status.hasPermission) errorCallback();
      },
      errorCallback);
  }
}

the console always says : 'Camera permission is not turned on' and no permission dialog shows.

then i searched again and found this solved question and it`s answer , so i installed 'cordova-plugin-diagnostic' and tried this code:

function requestPermission(){
    cordova.plugins.diagnostic.requestRuntimePermission(function(status){
        switch(status){
            case cordova.plugins.diagnostic.runtimePermissionStatus.GRANTED:
                console.log("Permission granted (or already granted) - call the plugin");
                // call SQLite plugin
                break;
            case cordova.plugins.diagnostic.runtimePermissionStatus.DENIED:
                console.log("Permission denied - ask again");
                alert("Come on user, we really need this. I'll ask again...");
                requestPermission();
                break;
            case cordova.plugins.diagnostic.runtimePermissionStatus.DENIED_ALWAYS:
                console.log("Permission permanently denied");
                alert("Well that's it, we're dead Jim");
                navigator.app.exitApp();
                break;
        }
    }, function(error){
        console.error("The following error occurred: "+error);
    }, cordova.plugins.diagnostic.runtimePermission.READ_PHONE_STATE);
}
requestPermission();

the app close and no dialog shows too, i think i can`t get what should this plugins do and how can i get the permission to open gallery. if can somebody give me full example to open gallery and pick image with permission it will be great help.

sorry for my English , and thanks for being patient .

like image 350
Mahmoud Mabrouk Avatar asked Dec 29 '16 00:12

Mahmoud Mabrouk


3 Answers

In config.xml file, try to add

enter image description here

and then build the app. It will automatically override AndroidManifest.xml file in platforms folder and thus the popup dialog with deny and allow button works fine.

Kindly see the below link for more reference.

https://inthecheesefactory.com/blog/things-you-need-to-know-about-android-m-permission-developer-edition/en

like image 41
Nivedha Lakshmanan Avatar answered Sep 30 '22 02:09

Nivedha Lakshmanan


I was able to get the permissions request to show by adding the relevant permissions and feature to platforms/android/AndroidManifest.xml.

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-feature android:name="android.hardware.camera" android:required="true" />
like image 178
Mark Harbison Avatar answered Sep 30 '22 03:09

Mark Harbison


I had a same issue dear . the requesting dialog box did not appear. after some concentration on my code, i found that, var permissions = cordova.plugins.permissions is defined in DeviceReady() function and in the other side i was calling checkPermissionCallback() as a callback function which is not in DeviceReady() function so var permissions is not defined in that function (scopes are different). so checkPermissionCallback have must been defined in DeviceReady() function.

like image 34
Abtin Shahkarami Avatar answered Sep 30 '22 02:09

Abtin Shahkarami