1
votes

I went through the documentation: https://www.codenameone.com/javadoc/com/codename1/ui/URLImage.html#fetch

By default an image is fetched lazily as it is asked for by the GUI unless the fetch() method is invoked in which case the IO code is executed immediately.

It looks like the storage file is not getting refreshed after fetch() is invoked. Here is the test I ran

1) I create a URLImage the first time with the following image: "https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png"

2) I run the application. The image is well displayed

3) I re run the application with a new URL image "http://awoiaf.westeros.org/images/thumb/9/93/AGameOfThrones.jpg/400px-AGameOfThrones.jpg" , but I still see the previous image although fetch() is being called!

    Form hi = new Form("Hi World");

    EncodedImage placeholder = EncodedImage.createFromImage(Image.createImage(hi.getWidth(), hi.getWidth() / 5, 0xffff0000), true);
    URLImage background = URLImage.createToStorage(placeholder, "400px-AGameOfThrones.jpg",
            "https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png");
    background.fetch();

    Label label = new Label();
    label.setIcon(background);
    hi.addComponent(label);
    hi.show();

Second run:

  Form hi = new Form("Hi World");

    EncodedImage placeholder = EncodedImage.createFromImage(Image.createImage(hi.getWidth(), hi.getWidth() / 5, 0xffff0000), true);
    URLImage background = URLImage.createToStorage(placeholder, "400px-AGameOfThrones.jpg",
            "http://awoiaf.westeros.org/images/thumb/9/93/AGameOfThrones.jpg/400px-AGameOfThrones.jpg");
    background.fetch();

    Label label = new Label();
    label.setIcon(background);
    hi.addComponent(label);
    hi.show();
1

1 Answers

1
votes

The documentation should probably be clarified, immediately doesn't mean synchronously so the fetch method will just add the image to the download queue but will not block until the image is physically present.

If you want that sort of behavior URL image is probably not the best solution. You can either use Util.downloadToStorage() or the Image download methods in ConnectionRequest coupled with addToQueueAndWait.