downloadIrlToStorageInBackground in ImageList model for imageViewer downloads&每次都覆盖图像

时间:2016-09-15 13:34:15

标签: codenameone

class ImageList implements ListModel<Image> {
    private int selection;
    private Image[] images;
    private EventDispatcher listeners = new EventDispatcher();

    public ImageList() {
        this.images = new EncodedImage[imageURLs.length];
    }

    public Image getItemAt(final int index) {
        if (images[index] == null) {
            images[index] = placeholderForTable;
            Util.downloadUrlToStorageInBackground(imageURLs[index], "list" + index, (e) -> {
                try {
                    images[index] = EncodedImage.create(Storage.getInstance().createInputStream("list" + index));
                    listeners.fireDataChangeEvent(index, DataChangedListener.CHANGED);
                } catch (IOException err) {
                    err.printStackTrace();
                }
            });
        }
        return images[index];
    }

    public int getSize() {
        return imageURLs.length;
    }

    public int getSelectedIndex() {
        return selection;
    }

    public void setSelectedIndex(int index) {
        selection = index;
    }

    public void addDataChangedListener(DataChangedListener l) {
        listeners.addListener(l);
    }

    public void removeDataChangedListener(DataChangedListener l) {
        listeners.removeListener(l);
    }

    public void addSelectionListener(SelectionListener l) {
    }

    public void removeSelectionListener(SelectionListener l) {
    }

    public void addItem(Image item) {
    }

    public void removeItem(int index) {
    }
}

protected void postMenuForm(Form f){

BusinessForumImagesConnection bfic = new BusinessForumImagesConnection();
bfic.businessForumImagesConnectionMethod(new ActionListener() {

@Override
public void actionPerformed(ActionEvent evt) {
    if (bfic.response != null) {
        for (int i = 0; i < imgLoop; i++) {
            HashMap hm = (HashMap) bfic.response.get(i);
            String imgUrl = (String) hm.get("imgUrl");
            imageURLs[i] = imgUrl;
        }
    }
  }
});

if (imageURLs != null) {
    ImageList imodel = new ImageList();
    ImageViewer iv = new ImageViewer(imodel.getItemAt(0));
    iv.setImageList(imodel);
    Container adsContainer = BoxLayout.encloseY(adsLabel, iv);

    slideIndex = 0;
    Runnable r = new Runnable() {
        public void run() {
            if (slideIndex < imodel.getSize()) {
                nextImage = (Image) imodel.getItemAt(slideIndex);
                if (nextImage != null) {
                    iv.setImage(nextImage);
                }
                slideIndex++;
            } else {
                slideIndex = 0;
            }
        }
    };
    if (uITimer == null) {
        uITimer = new UITimer(r);
    }
    if (uITimer != null) {
        uITimer.schedule(5000, true, f); //5 seconds
    }
    f.add(BorderLayout.SOUTH, adsContainer);

    adsContainer.setLeadComponent(adsLabel);
    adsLabel.addActionListener((e) -> {
        showForm("BusinessForum", null);
    });
  }
}

之前我使用过URLImage.createToStorage但是imageViewer没有正常工作,所以我使用了ImageList模型。但每次打开表单时,它都会重新下载imgs并将其覆盖在存储中,这会使应用程序变慢。我如何确定图像是否已经下载,它不会再次下载并且jst在imgViewer中显示它们?三江源

1 个答案:

答案 0 :(得分:0)

下载方法将始终下载,无论......

您需要检查Storage文件是否存在,如果存在,请加载该文件。

请参阅新厨房水槽中的WebServices / Dogs演示:http://www.codenameone.com/blog/kitchensink-ii.html