【发布时间】:2018-02-20 02:56:16
【问题描述】:
我正在使用cordova-plugin-file-transfer 和cordova-plugin-file 下载用户头像并在应用程序本地显示它们。我根据:https://ionicframework.com/docs/native/file-transfer/ 成功获取图像并将它们存储在file.dataDirectory 中。当我检查目录中的文件时,它说它存在......但是当我尝试将文件的绝对路径设置为 <img> 标记时,我什么也看不到。我正在动态地拉动路径,但即使我对其进行硬编码......什么也没有。
保存、加载头像的代码:
saveAvatar()
{
var downloadUrl = this.user.avatar + "?type=small&sz=75";
console.log("Downloading Avatar from: " + downloadUrl);
console.log("Storing file to: " + this.file.dataDirectory + this.user.id + '/avatar.jpg');
this.fileTransfer.download(downloadUrl, this.file.dataDirectory + this.user.id + '/avatar.jpg')
.then((entry) => {
console.log('~~~~~ download complete: ' + entry.toURL());
}, (error) => {
console.log('Unable to download avatar: ' + error);
});
}
loadAvatar()
{
var userDir = this.file.dataDirectory + this.user.id;
var fileName = 'avatar.jpg';
this.file.resolveDirectoryUrl(userDir)
.then((directoryEntry: DirectoryEntry) => {
console.log("Dir Resolved: " + directoryEntry.isDirectory);
this.file.getFile(directoryEntry, fileName, { create: false })
.then(file => {
console.log("File Found: " + file.toURL());
this.avatars[this.user.id] = file.toURL();
})
.catch(err => console.log('Unable to load avatar: ' + err));
})
.catch(err => console.log('Unable to Resolve Directory Entry: ' + JSON.stringify(err)));
}
存储图片的绝对路径如下:
file:///data/user/0/package_name_here/files/1/avatar.jpg
或尝试外部存储时:
file:///storage/emulated/0/Android/package_name_here/files/1/avatar.jpg
非常感谢任何帮助!
【问题讨论】:
标签: typescript ionic-framework cordova-plugins ionic3 cordova-plugin-file