Ionic navigator.camera.getPicture won't return image from PHOTOLIBRARY

I’ve added cordova-plugin-camera plugin to my ionic app, in order to use both of its advantages - take a picture using camera, and get image from the photo gallery.

Taking a picture using camera works perfectly on my android galaxy s3 device, but getting image from gallery returns NULL in the success result function.

I’ve tried using both $cordovaCamera.getPicture and navigator.camera.getPicture, both returns null as the result param in success method, after i select image on my device.

I’ve tried playing with all of the params, tried getting any of the 3 options of destinationType (DATA_URL, FILE_URL, NATIVE_URI), tried with saving/unsaving to gallery, edit/unedit, encoding type explicity of jpeg, mediaType PICTURE, etc… none of them worked with PHOTOLIBRARY or SAVEDPHOTOALBUM, but works fine with CAMERA

This is the code i wrote:

ver 1:

var options = {

destinationType: Camera.DestinationType.FILE_URI,
sourceType: Camera.PictureSourceType.PHOTOLIBRARY,
correctOrientation: true,
allowEdit: true
};

$cordovaCamera.getPicture(options)
.then(function (imageURI) {
if (!imageURI) {
console.log(‘getPicture: no image selected’);
return;
}
// upload image to server code goes here
});
ver 2:

navigator.camera.getPicture(onSuccess, onFail, 

{
destinationType: Camera.DestinationType.FILE_URI,
sourceType: Camera.PictureSourceType.PHOTOLIBRARY,
correctOrientation: true,
allowEdit: true
});

function onSuccess(imageData) {
console.log(imageData);
// upload image to server code goes here
}

function onFail(message) {
alert('Failed because: ’ + message);
}

can anyone advice what could be the problem? (why imageURI/imageData is null) maybe i miss some configuration somewhere? i’m out of ideas…

@ofershap… I have the same problem. Have you found a solution?

no, unfortunately i havn’t. My (ugly) solution was to use imagePicket on android devices, and getPicture only on iOS devices…

I think found a solution. In my case, this problem occurs when I use the latest version of cordova plugin camera. If I use the oldest org.apache.cordova.camera instead cordova-plugin-camera everything works fine. If you want, I made an example in my Github https://github.com/fajohann/ionic-camera-example.

installing old deprecated version will resolve installing the same plugin:

$ cordova plugin add org.apache.cordova.camera

Notice: org.apache.cordova.camera has been automatically converted to cordova-plugin-camera to be fetched from npm. This is due to our old plugins registry shutting down.
Fetching plugin “cordova-plugin-camera” via npm

i’ve tried to install specific versions going backward, and found that v1.2.0 is the last version that works on android (v2.0.0 and up returns null)

I was having the same problem on an Android 4.3 emulator while it was working fine on an Android 5.0 device. Removing correctOrientation: true from the options fixed it for me.

1 Like