Skip to content

Instantly share code, notes, and snippets.

@jbagaresgaray
Created February 8, 2017 07:15
Show Gist options
  • Save jbagaresgaray/0bc96b6d37c3066da13359340db37a9a to your computer and use it in GitHub Desktop.
Save jbagaresgaray/0bc96b6d37c3066da13359340db37a9a to your computer and use it in GitHub Desktop.
Ionic 2 Camera and Library
import { Component } from '@angular/core';
import { NavController, ActionSheetController, AlertController, Platform } from 'ionic-angular';
import { Camera, EmailComposer } from 'ionic-native';
/*
Generated class for the Camera page.
See http://ionicframework.com/docs/v2/components/#navigation for more info on
Ionic pages and navigation.
*/
@Component({
selector: 'page-camera',
templateUrl: 'camera.html'
})
export class CameraPage {
appPlatForm: any
picImage: any
constructor(public navCtrl: NavController, public actionSheetCtrl: ActionSheetController, public alertCtrl: AlertController, platform: Platform) {
this.appPlatForm = platform
}
ionViewDidLoad() {
console.log('Hello CameraPage Page');
}
showCameraAlert() {
let options = {
quality: 100,
destinationType: 0,
sourceType: 1,
allowEdit: true,
encodingType: 0,
saveToPhotoAlbum: false
};
if (this.appPlatForm.is('cordova')) {
Camera.getPicture(options).then((imageData) => {
// let base64Image = 'data:image/jpeg;base64,' + imageData;
this.picImage = 'base64:photo.png//' + imageData;
let email = {
to: '[email protected]',
cc: null,
bcc: null,
attachments: [this.picImage],
subject: 'Currently at a school event? Take some photos and let us know about how our students are performing',
body: 'I just took this picture. Check it out',
isHtml: true
};
EmailComposer.isAvailable().then((available: boolean) => {
if (available) {
EmailComposer.open(email);
} else {
this.alertCtrl.create({
title: 'Error',
subTitle: 'Email Composer is not available!!!',
buttons: ['OK']
}).present();
}
});
}, (err) => {
console.log('Error: ', err);
});
} else {
console.log('No cordova found');
}
}
showLibraryAlert() {
let options = {
destinationType: 1,
sourceType: 2,
};
if (this.appPlatForm.is('cordova')) {
Camera.getPicture(options).then((imageData) => {
// let base64Image = 'data:image/jpeg;base64,' + imageData;
this.picImage = imageData
let email = {
to: '[email protected]',
cc: null,
bcc: null,
attachments: [this.picImage],
subject: 'Currently at a school event? Take some photos and let us know about how our students are performing',
body: 'I just took this picture. Check it out',
isHtml: true
};
EmailComposer.isAvailable().then((available: boolean) => {
if (available) {
EmailComposer.open(email);
} else {
this.alertCtrl.create({
title: 'Error',
subTitle: 'Email Composer is not available!!!',
buttons: ['OK']
}).present();
}
});
}, (err) => {
console.log('Error: ', err);
});
} else {
console.log('No cordova found');
}
}
presentActionSheet() {
let actionSheet = this.actionSheetCtrl.create({
title: 'Choose photo',
buttons: [{
text: 'Take Photo',
handler: () => {
console.log('Take Photo clicked');
this.showCameraAlert()
}
}, {
text: 'From Library',
handler: () => {
console.log('From Library clicked');
this.showLibraryAlert()
}
}, {
text: 'Cancel',
role: 'cancel',
handler: () => {
console.log('Cancel clicked');
}
}]
});
actionSheet.present();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment