1
votes

It seems that if at the time when URLImage.createCachedImage is called to load and the image is not available (no connection, etc.), it won't be called again to reload when the connection comes back.

Description:

When the connection is unavailable, it shows a blank image. And it still shows the same blank image when the Display.getInstance().callSerially(...) is called again. It does not seem like it is invoking the connection to try to load the image but the blank image has apparently become the cache.

I'm not sure if I'm describing my problem properly, but here's the simplified question: how to deal with loading of image and handle events when the connection/server is unavailable? (I thought URLImage.createCachedImage knows if the image is not loaded and will try again.)

I have this piece of code to load Image:

protected static final Image loadImage(String imageAccessLocation) {
    int filenameIndex = imageAccessLocation.lastIndexOf("/");
    String filename = imageAccessLocation.substring(filenameIndex + 1);
    String imageAccessBaseLocation = Application.getInstance().getImagesAccessBaseLocation();
    String imageAccessURL = imageAccessBaseLocation + imageAccessLocation;
    int displayWidth = Display.getInstance().getDisplayWidth();
    EncodedImage imagePlaceholder = EncodedImage.createFromImage(Image.createImage(displayWidth, displayWidth / 5, 0xffff0000), true);
    Image image = URLImage.createCachedImage(filename, imageAccessURL, imagePlaceholder, FLAG_RESIZE_SCALE);
    return image;
}

protected static final Container prepareImageContainer(Item item) {
    Image image = item.getImage();
    if(image == null) {
        return null;
    }
    Image scaledImage = image.scaled(50, 50);
    Container imageContainer = new Container();
    imageContainer.add(scaledImage);
    return imageContainer;
}

private final void prepare() {
    Container imageContainer = prepareImageContainer(lookslike);
    Container textContainer = prepareTextDescription(lookslike);
    add(imageContainer);
    add(textContainer);
}
1

1 Answers

2
votes

You're right. If there is a network problem, URLImage will just silently fail.

If the URLImage hasn't finished downloading the image for whatever reason, its isAnimation() method will return true. This is a way to detect if it hasn't finished downloading. It doesn't tell you whether the download failed or just hasn't completed, but you could combine that with some sort of timeout to check if the image is downloaded, and, if not, replace it with a new URLImage for the same URL.

Not ideal, I know. You can file an RFE in the issue tracker and we'll evaluate the request.