【发布时间】:2017-03-06 02:18:32
【问题描述】:
我正在尝试在我的 Ionic 2 应用中上传一张获取照片。我成功运行相机并将照片保存在 Firebase 中,但没有在我的应用程序上获取和显示它。我不确定我做对了,保存在 Firebase 中。
我把我的代码放在这里。
add-note.ts
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { Camera } from 'ionic-native';
import {NotesData} from "../../providers/notes-data";
/*
Generated class for the AddNote page.
See http://ionicframework.com/docs/v2/components/#navigation for more info on
Ionic pages and navigation.
*/
@Component({
selector: 'page-add-note',
templateUrl: 'add-note.html'
})
export class AddNote {
public notePicture: any;
constructor(public navCtrl: NavController,public notesData:NotesData) {}
ionViewDidLoad() {
}
addNote() {
this.notesData.addPhoto(this.notePicture);
this.navCtrl.pop();
}
takePicture(){
Camera.getPicture({
quality : 95,
destinationType : Camera.DestinationType.DATA_URL,
sourceType : Camera.PictureSourceType.CAMERA,
allowEdit : true,
encodingType: Camera.EncodingType.PNG,
targetWidth: 500,
targetHeight: 500,
saveToPhotoAlbum: true
}).then(imageData => {
this.notePicture = imageData;
}, error => {
console.log("ERROR -> " + JSON.stringify(error));
});
}
}
note-data.ts //笔记提供者将照片上传到firebase
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';
import firebase from 'firebase'
/*
Generated class for the NotesData provider.
See https://angular.io/docs/ts/latest/guide/dependency-injection.html
for more info on providers and Angular 2 DI.
*/
@Injectable()
export class NotesData {
//user data
public currentUser: any;
public profilePictureRef: any;
//notedata
public notesList: any;
//firebase data
public photoRef:any;
constructor() {
this.currentUser = firebase.auth().currentUser.uid;
this.fireRef=firebase.database().ref();
this.photoRef=firebase.database().ref('users/'+this.currentUser+'/photos');
this.notesList =
}
addPhoto(notePicture:any){
this.photoRef.push({id:"data:image/jpeg;base64,"+notePicture});
}
}
【问题讨论】:
标签: ionic-framework firebase firebase-realtime-database ionic2