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 .
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" />
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.
In config.xml file, try to add
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
I'm not familiar with the plugin you used, but I was able to use the cordova-plugin-settings-hook with these kind of entries in my config.xml to address a similar issue:
<platform name="android">
<config-file parent="/*" platform="android" target="AndroidManifest.xml">
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
To use the above settings in your config.xml, you also need to edit the widget
attribute to include an additional attribute: xmlns:android="http://schemas.android.com/apk/res/android"
.
After setting the android target version to api 23 in cordova config.xml
<preference name="android-targetSdkVersion" value="23" />
<preference name="android-minSdkVersion" value="19" />
you can proceed to indicate the required permissions in android/androidManifest.xml file
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera" android:required="true" />
with this setting, you can use any of the android permission plugins for cordova e.g
https://www.npmjs.com/package/cordova-plugin-android-permissions
Depending on what you want to achieve, this should solve most or all of your problems
just yesterday I found this same problem and started looking in the documentation of npm (I leave the link: https://www.npmjs.com/package/cordova-plugin-permission), but it suits my need to find the solution.
var permissions = plugins.Permission;
permissions.has('android.permission.CAMERA', function (status) {
if(!status.has) {
var errorCallback = function() {
console.warn('Camera permission is not turned on');
}
permissions.request(
'android.permission.CAMERA',
function(status) {
if(!status.has) errorCallback();
},
errorCallback);
}
}, alert);