valix85
3/24/2016 - 9:52 AM

Cordova camera - Choose camera OR photo library

Cordova camera - Choose camera OR photo library

//
// you need the actionsheet plugin, image picker plugin and the camera plugin for this code to work
//

/**
 * displays an action sheet for the user to select a photo from
 * the gallery or using the camera
 *
 * @param _event {Object} information from the webview on the event
 *
 */
function selectPhoto(_event) {

    if (_event.keyCode == 13 || _event.x == 0) {
        //event.preventDefault();
        return true;
    }
    var options = {
        'buttonLabels': ['Take Picture', 'Select From Gallery'],
        'addCancelButtonWithLabel': 'Cancel'
    };
    window.plugins.actionsheet.show(options, function (_btnIndex) {
        if (_btnIndex === 1) {
            doGetProfilePhoto();
        } else if (_btnIndex === 2) {
            doGetGalleryPhoto();
        }
    });
}


/**
 * displays the photo gallery for the user to select an image to
 * work with
 */
function doGetGalleryPhoto() {
    CameraService.getPicturesFromGallery().then(function (imageURI) {
        console.log(imageURI);
        vm.lastPhoto = imageURI;
        vm.newPhoto = true;
    }, function (err) {
        console.log(err);
        vm.newPhoto = false;
        alert("Buddy Connector", "Error Getting Photo " + err);
    });
}

/**
 * displays the camera for the user to select/take a photo
 */
function doGetProfilePhoto() {
    var picOptions = {
        destinationType: navigator.camera.DestinationType.FILE_URI,
        quality: 75,
        targetWidth: 500,
        targetHeight: 500,
        allowEdit: true,
        saveToPhotoAlbum: false
    };


    CameraService.getPicture(picOptions).then(function (imageURI) {
        console.log(imageURI);
        vm.lastPhoto = imageURI;
        vm.newPhoto = true;

    }, function (err) {
        console.log(err);
        vm.newPhoto = false;
        alert("Buddy Connector", "Error Getting Photo " + err);
    });
}