无法在Ionic的cordova文件中设置img src

时间:2017-09-11 19:17:18

标签: typescript ionic-framework cordova-plugins ionic3 cordova-plugin-file

我正在使用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

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:2)

找出问题所在。虽然我在Android设备上运行,但是我在离子中运行它并不适用于所有的cordova插件。因此,我只需将其ionic cordova run android -l -c运行,而不是ionic cordova android