Time for action – selecting a photo from the photo library
This example is available for download or browse at https://github.com/myleftboot/Chapter-7-camera/tree/emailFromLibrary. Perform the following steps to select a photo from the photo library:
Open the code from the previous example.
Add a button to the existing layout:
var emailFromLibrary = Ti.UI.createButton({title: 'Email from photo library'}); options.add(emailFromLibrary);
Add a function that will create a new e-mail with the subject and add the picture as an attachment:
function emailPiccy(_args) { var toSend = Ti.UI.createEmailDialog({}); toSend.subject = 'A photo I took earlier'; toSend.messageBody = 'Thinking of you...'; toSend.addAttachment(_args.media); toSend.open(); }
Lastly, add an event listener to the button added in an earlier step. This code will open up the photo gallery browser. The
success
attribute defines the code that should be run when a photo is selected. If the user cancels, nothing happens...