【发布时间】:2018-04-27 02:58:10
【问题描述】:
我对 Ionic 和 Cordova 非常陌生,在过去的几天里,我正在尝试下载我上传到 Firebase 存储中的图像。我想通过我的移动应用程序传输图像并将其存储在我的移动设备中。我已经安装了执行此操作所需的所有插件。我创建了两个按钮。第一个按钮是在我的应用程序中显示图像,第二个按钮是在我的设备中下载图像。源代码在我的 storage.html 中
<ion-header>
<ion-navbar>
<ion-title>storage</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
<button ion-button (click)="display()">Display</button>
<button ion-button (click)="download()">Download</button>
<img src="{{imgsource}}">
</ion-content>
功能在我的 storage.ts 中
import { Component, NgZone } from '@angular/core';
import { NavController, Platform, AlertController } from 'ionic-angular';
//import {File,Transfer} from 'ionic-native';
import {FileTransferObject } from '@ionic-native/file-transfer';
import {TransferObject} from '@ionic-native/transfer';
import {Transfer} from '@ionic-native/transfer';
import {File} from '@ionic-native/file';
import firebase from 'firebase';
declare var cordova: any;
@Component({
selector: 'storage-home',
templateUrl: 'storage.html',
providers: [Transfer, TransferObject, File]
})
export class StoragePage {
storageDirectory: string = '';
fileTransfer: FileTransferObject;
nativepath: any;
firestore = firebase.storage();
imgsource: any;
constructor(public navCtrl: NavController, public platform: Platform, public alertCtrl: AlertController, public zone: NgZone) {
this.platform.ready().then(() => {
// make sure this is on a device, not an emulation (e.g. chrome tools device mode)
if(!this.platform.is('cordova')) {
return false;
}
if (this.platform.is('ios')) {
this.storageDirectory = cordova.file.documentsDirectory;
}
else if(this.platform.is('android')) {
this.storageDirectory = cordova.file.dataDirectory;
}
else {
// exit otherwise, but you could add further types here e.g. Windows
return false;
}
});
}
display() {
this.firestore.ref().child('image.jpg').getDownloadURL().then((url) => {
this.zone.run(() => {
this.imgsource = url;
this.fileTransfer.download(url,'image.jpg').then((entry) => {
console.log('download complete: ' + entry.toURL());
}, (error) => {
// handle error
});
})
})
}
downlad() {
this.firestore.ref().child('image.jpg').getDownloadURL().then((url) => {
this.zone.run(() => {
this.imgsource = url;
this.fileTransfer.download(url,cordova.file.dataDirectory +'image.jpg').then((entry) => {
console.log('download complete: ' + entry.toURL());
}, (error) => {
// handle error
});
})
})
}
}
当我在我的设备上安装应用程序时,我可以看到我的图像,显示按钮可以完美运行。但问题在于下载按钮,因为什么都没有发生,我不知道它是否正常工作,因为我无法在设备的任何地方找到我的图像。谁能指导一下?
感谢您的问候
【问题讨论】:
标签: android cordova firebase ionic-framework file-transfer