Camera, ImagePicker permission to access media

I am using using cordova plugins for Camera & ImagePicker.

cordova-plugin-camera
cordova-plugin-image-picker

They are both working fine, however in order to get the Camera to work, I first need to give the phone permission to allow my app to access media, which is as expected.

Allow <appname> to access photos, media, and files on your device?
DENY ALLOW

When I access the ImagePicker, it fails, if I have not first allowed my app access to the media by first accessing the Camera. The ImagePicker does not prompt the user to allow access to media, it just fails.

Does anyone know how I can get the ImagePicker to prompt the user to allow access to media?

Thanks

Could you provide some more information? What platform does this happen on? iOS or Android? Also, what version of the plugin,platform are you testing against?

Hi mhartington, thank you for your reply.

I deploy the apk file to an Android 6.0.1 devise.

Installed plugins:

cordova-plugin-camera 2.2.0 "Camera"
cordova-plugin-compat 1.0.0 "Compat"
cordova-plugin-console 1.0.3 "Console"
cordova-plugin-crop 0.1.0 "CropPlugin"
cordova-plugin-device 1.1.2 "Device"
cordova-plugin-image-picker 1.1.1 "ImagePicker"
cordova-plugin-splashscreen 3.2.2 "Splashscreen"
cordova-plugin-statusbar 2.1.3 "StatusBar"
cordova-plugin-whitelist 1.2.2 "Whitelist"
ionic-plugin-keyboard 2.2.1 "Keyboard"

To generate the apk, I do cordova build. I am using Ionic 2.0.0-beta.32.

My code:

pickImage() {
    ImagePicker.getPictures({
        maximumImagesCount: 1,
        width: 400,
        height: 400,
        quality: 75
    }).then((results) => {
        this.toBase64(this, results[0], function (that, base64Img) {
            that.zone.run(() => {
                that.base64Image = base64Img;
            });
        });
    }, (error) => {
        console.log("ERROR -> " + JSON.stringify(error));
        alert("ERROR: " + JSON.stringify(error));
    });
}
toBase64(that, url, callback) {
    var xhr = new XMLHttpRequest();
    xhr.responseType = 'blob';
    xhr.onload = function () {
        var reader = new FileReader();
        reader.onloadend = function () {
            callback(that, reader.result);
        }
        reader.readAsDataURL(xhr.response);
    };
    xhr.open('GET', url);
    xhr.send();
}

I appreciate your help. Thanks

Hmm, you might need to install the diagnostic plugin or the compat plugin.

The compat plugin is much simpler (just install and you’re done) and handles the permissions between older android and android 6x, which has a much different permissions API.

Hi Mike,

I already have cordova-plugin-compat installed, it came with the original Ionic2 install.

Do you have any other ideas?

I have read elsewhere of other people having the same problem, but haven’t wound a solution yet.

Thanks for your help.

Mike,

Looks like what you were saying about cordova-plugin-compat may be correct for Android 6.

https://developer.android.com/training/permissions/requesting.html?hl=ru

e.g.

// Assume thisActivity is the current activity
int permissionCheck = ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.WRITE_CALENDAR);

Do you know in Ironic 2 how I would import ContextCompat cordova-plugin-compat?

Here are a list of permissions:

https://developer.android.com/reference/android/Manifest.permission.html

Do you know which one is for accessing the photo gallery?

Thanks

I ran in to the exact same issue with the camera and gallery access on Android 6.0.1 device.

THE FIX
DONT : Do not request access for the CAMERA, it leads in to the issue of permission DENIED_ALWAYS (look in to Cordova Diagnostic Plugin Pull 33 for more details). I did try setting the below in the AndroidManifest.xml, but nothing seems to work for CAMERA.

<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />

DO : Access the runtime permission for the WRITE_EXTERNAL_STORAGE.

Following along your lines of code

var cpd = cordova.plugins.diagnostic;

function success(status) {
    if (cpd.permissionStatus.GRANTED === status) {
        pickImage();
    } else {
        alert('Allow the requested permission');
    } 
}

function failure(error) {
    console.log('handle error');
}

cpd.requestRuntimePermission(
    success, failure, cpd.runtimePermission.WRITE_EXTERNAL_STORAGE);

Hi neb,

This is what I ended up doing, which works well for me. I use the camera plugin to do both photos and gallery selection. I have posted my code here.

1 Like

I recently had a similar issue with image-crop plugin for ionic v1. In my own case, after I compiled my app and installed the apk on an android phone running on version 6.0.1. The application closes abruptly whenever I tried to select images from the phone’s gallery for cropping. Though it didn’t exhibit the behavior when I was testing on the ionic view app and in the avd emulator I had setup in android studio.

Anyway to cut the story short I came across a post which suggested I add this two tags to the android manifest file, which solved the problem in my own case:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
1 Like

Its done and working now, thanks all

None of the solutions work for me. I’m using ionic 3 with the latest Cordova and few different plugins that are accessing camera: barcode, camera, and gallery plugin. Barcode and gallery are working fine. They prompt properly for the permissions and accessing fine.
Camera throws error 20. I tested on a couple different Android phones( one had 6 and one had 8) and the behavior was the same.
I tried also using the diagnostic plugin mentioned above and ask for external storage authorization. I’m getting success back.
Any ideas?

Turns out that hockeyapp plugin was actually causing the problem. Solution from this thread actually helped:
https://github.com/bitstadium/HockeySDK-Cordova/issues/100

when i run this code the following error occurs:

cmd: Command failed with exit code 1 Error output:
D:\images\platforms\android\app\src\main\java\com\synconset\ImageFetcher.java:293: error: Entry is not public in LinkedHashMap; cannot be accessed from outside package
protected boolean removeEldestEntry(LinkedHashMap.Entry<Integer, Bitmap> eldest) ^
Note: D:\images\platforms\android\app\src\main\java\com\synconset\MultiImageChooserActivity.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: D:\images\platforms\android\app\src\main\java\com\synconset\MultiImageChooserActivity.java uses unchecked or unsafe operations.

Can you help me??

J’ai la meme erreur sur v4