简体   繁体   中英

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 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..

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 cordova-plugin-camera@1.2.0

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM