Search code examples
javascriptandroidjquerycordovaphonegap-plugins

Phonegap image picker from gallery not working properly


I want to use a photo from gallery and then crop it. I am using this plugin. It has absolutely no documentation so I need a bit help. On button click, I want to open the gallery and select an image and then crop it too. I made a function in my myapp.js

function uploadImage(){
    window.imagePicker.getPictures(
        function(results) {
            for (var i = 0; i < results.length; i++) {
                console.log('Image URI: ' + results[i]);
            }
        }, function (error) {
            console.log('Error: ' + error);
        }, {
            maximumImagesCount: 10,
            width: 800
        }
    );
}

and i am calling it on button click.

<a href="#" onClick="uploadImage();">Upload</a>

But my app crashes.

Unfortunately, App has stopped working.

What should I do?


Solution

  • You can use phonegap default camera plugin to get image and crop it. code is below which you can get easily from phongegap docs available in its official site.

    function uploadImage(){
        navigator.camera.getPicture(onSuccess, onFail, { quality: 50,
                    sourceType: Camera.PictureSourceType.PHOTOLIBRARY, 
                    allowEdit: true,
                    destinationType: Camera.DestinationType.FILE_URI
                    });
    }
    function onSuccess(imageURI){
                     var image = document.getElementById('smallimage');
                     image.src = "data:image/jpeg;base64," +imageURI;
                }
    
                function onFail(message){
                }
    

    allowEdit option will give cropping option and you can even give fixed cropping width and height through below option.

         targetWidth: 400,targetHeight: 250,