【发布时间】:2016-06-17 20:48:23
【问题描述】:
我正在 Ionic2 中开发应用程序,我正在使用“cordova-plugin-file-transfer”下载图像,并使用以下代码感谢 (ionic-2-file-transfer-example):
downloadImage(image, name) {
this.platform.ready().then(() => {
const fileTransfer = new FileTransfer();
let targetPath; // storage location depends on device type.
// 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')) {
targetPath = cordova.file.documentsDirectory + name;
}
else if(this.platform.is('android')) {
targetPath = cordova.file.dataDirectory + name;
}
else {
// do nothing, but you could add further types here e.g. windows/blackberry
return false;
}
fileTransfer.download(encodeURI(image), targetPath,
(result) => {
//Here i need to load it back
this.img = targetPath; // <<-- ERROR -> Can't load from device
},
(error) => {
console.log("error: "+error);
}
);
});
}
它工作得很好(图像保存在设备上),问题是,我需要在我的应用程序中加载这个图像(我存储在我的设备中)。
我在这个cordova库中看到了以下代码:
function readBinaryFile(fileEntry) {
fileEntry.file(function (file) {
var reader = new FileReader();
reader.onloadend = function() {
console.log("Successful file read: " + this.result);
// displayFileData(fileEntry.fullPath + ": " + this.result);
var blob = new Blob([new Uint8Array(this.result)], { type: "image/png" });
displayImage(blob);
};
reader.readAsArrayBuffer(file);
}, onErrorReadFile);
}
但是,我无法成功使用它(或将其嵌入到我的代码中),任何人都知道如何加载我存储的图像并将其推送到我的 ionic 代码中:
<img [src]="avatar" style="margin: auto;" alt="Profile">
this.avatar = "???";
谢谢!!
【问题讨论】:
标签: javascript cordova cordova-plugins ionic2