首页 > 解决方案 > 从存储加载图像

问题描述

我有一个使用 URLImage 类加载的图像。这是代码:

EncodedImage placeholder = EncodedImage.createFromImage(Image.createImage(getWidth()/2, getWidth()/2 , 0xF92D2D), true);
Image originImg = URLImage.createToStorage(placeholder, imgFileName, photo_url);

据我了解,此图像正在使用createToStorage()方法保存到存储中。但是,下次加载此表单时,我不想再次下载图像,我想从存储中获取它,因为它更快。所以我做了什么,我添加了检查:

if (Storage.getInstance().exists(imgFileName)) {
        // Take the image from the Storage
    originImg = Image.createImage(Storage.getInstance().createInputStream(imgFileName));
} else {
        // Load the image with URLImage class.
}

但是,似乎我的文件从未保存到存储中。有什么问题?

标签: codenameone

解决方案


你不需要这样做。URLImage如果文件已经存在,则从存储中无缝加载文件。您可以打开网络监视器并查看是否对该特定图像的 URL 发出了请求。

我不确定为什么该Storage.getInstance().exists(imgFileName)方法会失败。我假设图像名称在不同的执行或类似的东西中是不同的。您可以看到在您的.cn1目录中创建了图像。


推荐阅读