【问题标题】:Unable to set img src from cordova file in Ionic无法从 Ionic 中的 cordova 文件设置 img src
【发布时间】:2018-02-20 02:56:16
【问题描述】:

我正在使用cordova-plugin-file-transfercordova-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


    【解决方案1】:

    找出问题所在。虽然我在安卓设备上运行,但我在 ionic 中实时运行它,它不适用于所有 cordova 插件。所以不用ionic cordova run android -l -c,我只需要以ionic cordova android 运行它。

    【讨论】:

    • 哦..这是否意味着Plugins 无法使用-l 在设备本身上工作?
    • 不,我确实有一些插件可以与l 一起使用,这让我很困惑,但我注意到有些插件只有在我没有它的情况下才能运行。所以我不知道这有什么押韵或理由,但很高兴知道,因为它可以在未来省去一些麻烦。
    • 是的,当我们需要测试Plugins 时,使用这个ionic cordova run android --prod --device 总是好的。
    猜你喜欢
    • 1970-01-01
    • 2017-12-13
    • 2018-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    • 2015-07-19
    相关资源
    最近更新 更多