Skip to content

Instantly share code, notes, and snippets.

@aaronksaunders
Last active July 31, 2018 08:14
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save aaronksaunders/e522c2466619d99dc799 to your computer and use it in GitHub Desktop.
Save aaronksaunders/e522c2466619d99dc799 to your computer and use it in GitHub Desktop.
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);
});
}
@hnakhuva
Copy link

What to do if I want to select multiple photos from photo library?? It allowes only 1 photo to be selected at a time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment