Select pictures on iOS/Android and send them to RESTfull api

Hi,

We’ve built an Ionic app where a user could change this avatar which used across the app. We used the https://github.com/wymsee/cordova-imagePicker.git plugin, but the plugin only gave us the local path to the image, where we needed the image data so we could send the data to our server where we could persist it.

We putted our the solution on GitHub: https://github.com/brianvandelden/acato-service-image

var options = {
    maximumImagesCount: 1,
    width: 100,
    height: 100,
    quality: 100
};

$acatoImage.getPictures(options).then(function(results) {
    // loop true results
    for(var i = 0; i < results.length; i++){
        $acatoImage.getImageData(results[i]).then(function(imageData) {
            console.log(imageData, 'imageData');
            // send imageData to server X
        }, function(error) {
            alert(error, 'Error sending imageData to server');
        });
    }
}, function(error) {
    alert(error, 'Error getting pictures');
});
1 Like

Why don’t you use Cordova File API plugin to access this file, convert it to BASE64 string and send it remotely?

Find mentioned plugin here: http://www.gajotres.net/useful-cordova-plugins-for-your-ionic-application-examples/ plugin No 4.

You’ll also find a working example.

http://plugins.cordova.io/#/package/org.apache.cordova.camera

I used this plugin to take a picture on the device, and then save the base64 encoded string, which is returned by the cordova plugin.

You can setup sourceType to change the source from camera to for example ‘PHOTOLIBRARY’.

And DestinationType to DATA_URI for base64 encoded images.

Because we needed the multi file (image) selector.

This is also a viable solution mainly because it require only one plugin.

Though it has one problem, certain Android devices will not work with sourceType “PHOTOLIBRARY”. For some reason, instead of a photo library it will access certain 3rd party photo library applications leading to error.

Hi inv123,

Nice one!.

We used this methode a while ago, and this plugin you mentioned wasn’t there in that time, but looks that it does the job. Is it also supporting multi image selection? Couldn’t find it in the docs.

So what’s the problem? If this plugin can return an array of image locations you will process them in the loop.

Yes that’s what we did.

Yeah, i think it doesn’t support multi select.

It happens when a user associate a new application to a specific file type, like open .pdf files with some kind of a pdf reader, not the default built in, isn’t it?