2
votes

I've added cordova-plugin-camera plugin to my ionic app, in order to use both of its adnavtages - 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 like described here and navigator.camera.getPicture from this example, 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..

2
Confirm that you have the plugin installed. If you don't it will return null. Run cordova plugin add cordova-plugin-camera, just in case. But now I'm facing another issue: I get to the screen to select the image but after that it fails retrieving the image: Error retrieving image.Andre
The Error retrieving image. is a known issue. It may occur depending on the app that you use to pick the image to import.Andre
He's got the plugin installed fine, because it's calling the getPicture onSuccess callback.Chris Rae

2 Answers

2
votes

I've asked the same question in Ionic framework forum and fajohann answered that when he has installed an older version of the plugin it worked fine. I've tried it myself, installing previous versions one by one until i made it working. I've found out that the latest version that support android (doesnt return NULL when picking an image) is v1.2.0

In order to install it you should run the command:

ionic plugin add [email protected]

2
votes

Using an old version of the plugin is not a good solution, when something doesn't work you have to open an issue on http://issues.cordova.io giving all the relevant information you can provide, so developers can know about the issue and fix it.

Luckily somebody reported the issue https://issues.apache.org/jira/browse/CB-10625 and it was fixed.

The changes will be available on the version 2.1.1 of the plugin, but it has not been released yet. In the meantime you can install the plugin from the github url

ionic plugin add https://github.com/apache/cordova-plugin-camera