Skip to content Skip to sidebar Skip to footer

Choose Image From Android Image Gallery - Phonegap Not Working

For Android in PhoneGap 2.8.0: I need to get image from phone gallery and need to insert in app... My code: HEAD: ); largeImage.style.display = 'block'; largeImage.src = imageURI; } functiononPhotoDataSuccess(imageURI) { var imgProfile = document.getElementById('imgProfile'); imgProfile.src = imageURI; if(sessionStorage.isprofileimage==1) { getLocation(); } movePic(imageURI); } functiononFail(message) { alert('Failed because: ' + message); } functionmovePic(file) { window.resolveLocalFileSystemURI(file, resolveOnSuccess, resOnError); } functionresolveOnSuccess(entry) { var d = newDate(); var n = d.getTime(); var newFileName = n + ".jpg"; var myFolderApp = "MyAppFolder"; window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSys) { fileSys.root.getDirectory( myFolderApp, {create:true, exclusive: false}, function(directory) { entry.moveTo(directory, newFileName, successMove, resOnError); }, resOnError); }, resOnError); } functionsuccessMove(entry) { sessionStorage.setItem('imagepath', entry.fullPath); } functionresOnError(error) { alert(error.code); } functioncapturePhotoEdit() { navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 20, allowEdit: true, destinationType: destinationType.DATA_URL }); } functiongetPhoto(source) { navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 50, destinationType: destinationType.FILE_URI, sourceType: source }); } </script>

My html

<buttononclick="capturePhoto();">Capture Photo</button><buttononclick="getPhoto(pictureSource.PHOTOLIBRARY);">From Gallery!!</button>

Solution 2:

try this link i hope it might be help you

http://tympanus.net/codrops/2010/05/27/awesome-mobile-image-gallery-web-app/

See the api

http://docs.phonegap.com/en/2.8.0/index.html

and you also try like

if Camera.sourceType = Camera.PictureSourceType.PHOTOLIBRARY or Camera.PictureSourceType.SAVEDPHOTOALBUM, then a photo chooser dialog is shown, from which a photo from the album can be selected.

Solution 3:

Hi blow is working code for Camera and gallery. I use different function for both purpose because using the device capture you can capture the multiple image .

i hope it will help you.

    <select>
    <option>--Select--</option><optiononclick="GetImage(1)">Camera</option><optiononclick="GetImage(2)">Library</option><optiononclick="GetImage(3)">Album</option>
    </select>


    //// fuction to select which type of upload you want to performGetImage(index)
    {
        switch (index)
        {
            case1:
                captureImage();
                break;
            case2:
                getPhoto('pictureSource.PHOTOLIBRARY');
                break;
            case3:
                getPhoto('pictureSource.SAVEDPHOTOALBUM');
                break;

        }

    }
    //// fuction calle from camera capture successfunctioncaptureSuccess(mediaFiles) {
        var i, len;
        debugger;
        for (i = 0, len = mediaFiles.length; i < len; i += 1) {
            uploadFile(mediaFiles[i]);
        }
    }

    // Called if something bad happens.// functioncaptureError(error) {
        var msg = 'An error occurred during capture: ' + error.code;
        navigator.notification.alert(msg, null, 'Uh oh!');
    }

    //// function to camputure image from camera//functioncaptureImage() {
        // Launch device camera application, // allowing user to capture up to 2 images
        navigator.device.capture.captureImage(captureSuccess, capturetError, { limit: 1 });
    }

    //// Upload files to serverfunctionuploadFile(mediaFile) {
        var ft = newFileTransfer();
        path = mediaFile.fullPath;
        name = mediaFile.name;
////your s urlvar objUrl =www.xyx.com;         

        ft.upload(path,
            objUrl,
            function (result) {
                alert("Success");


            },
            function (error) {
                alert('Error uploading file ' + path + ': ' + error.code);
            },
            { fileName: name });
    }

    //// fuction to capture image from Gallery or PHOTOLIBRARYfunctiongetPhoto(source) {
        // Retrieve image file location from specified sourcevar cameraSource;
        if (source = "pictureSource.PHOTOLIBRARY") {
            cameraSource = pictureSource.PHOTOLIBRARY;
        }
        if (source = "pictureSource.PHOTOLIBRARY") {
            cameraSource = pictureSource.SAVEDPHOTOALBUM;
        }

        navigator.camera.getPicture(CaptureImageSuccess, onFail, {

            quality: 50,
            destinationType: destinationType.FILE_URI,
            sourceType: cameraSource
        });
    }


    //// Upload files to server from GalleryfunctionCaptureImageSuccess(fileURI) {
        ////your service urlvar objUrl =www.xyx.com;         

        var options = newFileUploadOptions();
            options.fileKey = "file";
            options.fileName = fileURI.substr(fileURI.lastIndexOf('/') + 1)+".jpg";
            options.mimeType = "image/jpeg";
            options.params = {}; // if we need to send parameters to the server requestvar ft = newFileTransfer();
            //ft.upload(fileURI, encodeURI(), win, fail, options);

            ft.upload(fileURI,
                objUrl,
                function (result) {
                    alert("Success");

                },
                function (error) {
                    alert('Error uploading file ' + path + ': ' + error.code);
                },
                options);
    };

Post a Comment for "Choose Image From Android Image Gallery - Phonegap Not Working"