我无法将图片上传到Firebase。需要帮助。
有谁熟悉在Ionic 4.0中如何上传图片到Firebase?以下是我曾经在Ionic 2中使用的代码,但现在当我点击上传图片到Firebase的按钮时,它需要大约30秒才能响应(click),然后它从未将图片上传到Firebase。
我尝试使用此教程中的示例,但无法摆脱与ImagePicker相关的错误。https://ionicthemes.com/tutorials/about/ionic-firebase-image-upload 非常感谢任何帮助。
<ion-card-content>
<div>
<img src="assets/img/add-an-image.png" (click)="selectPhoto()"/>
</div>
</ion-card-content>
constructor(
private afAuth: AngularFireAuth,
private camera: Camera) {
this.afAuth.authState.subscribe(user => {
this.userId = user.uid;
this.myPhotosRef = firebase.storage().ref(`/Photos/${ this.userId }/`);
});
}
selectPhoto(): void {
const options: CameraOptions = {
quality: 100,
destinationType: this.camera.DestinationType.FILE_URI,
encodingType: this.camera.EncodingType.JPEG,
mediaType: this.camera.MediaType.PICTURE,
sourceType: this.camera.PictureSourceType.PHOTOLIBRARY,
};
this.camera.getPicture(options).then((imageData) => {
console.log(options, 'get pic');
this.myPhoto = imageData;
this.uploadPhoto(this.myPhoto);
}, error => {
console.log('ERROR -> ' + JSON.stringify(error));
});
}
private uploadPhoto(photoName: string): void {
this.myPhotosRef.child(photoName)
.putString(this.myPhoto, 'base64', { contentType: 'image/png' })
.then((savedPicture) => {
this.myPhotoURL = savedPicture.downloadURL;
});
}