Search in sources :

Example 6 with ImageDownloadService

use of com.codename1.io.services.ImageDownloadService in project CodenameOne by codenameone.

the class ImageDownloadService method createImageToStorage.

/**
 * Constructs an image request that will automatically populate the given Label
 * when the response arrives, it will cache the file locally to the Storage
 *
 * @param url the image URL
 * @param l the Label that should be updated when the data arrives
 * to just use storage and the url as the key
 * @param cacheId a unique identifier to be used to store the image into storage
 * @param toScale the scale dimension or null
 * @param priority the priority for the task
 */
private static void createImageToStorage(final String url, final Label l, final String cacheId, final boolean keep, final Dimension toScale, final byte priority, final Image placeholder, final boolean maintainAspectRatio) {
    if (Display.getInstance().isEdt()) {
        Display.getInstance().scheduleBackgroundTask(new Runnable() {

            public void run() {
                createImageToStorage(url, l, cacheId, keep, toScale, priority, placeholder, maintainAspectRatio);
            }
        });
        return;
    }
    Image im = cacheImage(cacheId, keep, null, toScale, placeholder, maintainAspectRatio);
    if (im != null) {
        if (!fastScale && toScale != null) {
            im = scaleImage(im, toScale, defaultMaintainAspectRatio);
        }
        final Image i = im;
        Display.getInstance().callSerially(new Runnable() {

            public void run() {
                l.setIcon(i);
                Form f = l.getComponentForm();
                if (f != null) {
                    f.revalidate();
                }
            }
        });
        return;
    }
    // image not found on cache go and download from the url
    ImageDownloadService i = new ImageDownloadService(url, l);
    i.maintainAspectRatio = maintainAspectRatio;
    i.setDuplicateSupported(true);
    i.cacheImages = true;
    i.toScale = toScale;
    i.cacheId = cacheId;
    i.placeholder = placeholder;
    i.setPriority(priority);
    i.setFailSilently(true);
    NetworkManager.getInstance().addToQueue(i);
}
Also used : Form(com.codename1.ui.Form) EncodedImage(com.codename1.ui.EncodedImage) Image(com.codename1.ui.Image) FileEncodedImage(com.codename1.components.FileEncodedImage) StorageImage(com.codename1.components.StorageImage)

Example 7 with ImageDownloadService

use of com.codename1.io.services.ImageDownloadService in project CodenameOne by codenameone.

the class ImageDownloadService method createImageToStorage.

/**
 * Constructs an image request that will automatically populate the given list
 * when the response arrives, it will cache the file locally as a file
 * in the file storage.
 * This assumes the GenericListCellRenderer style of
 * list which relies on a map based model approach.
 *
 * @param url the image URL
 * @param targetList the list that should be updated when the data arrives
 * @param targetModel the model
 * @param targetOffset the offset within the list to insert the image
 * @param targetKey the key for the map in the target offset
 * @param cacheId a unique identifier to be used to store the image into storage
 * @param keep if set to true keeps the file in RAM once loaded
 * @param scale the scale of the image to put in the List or null
 */
private static void createImageToStorage(final String url, final Component targetList, final ListModel targetModel, final int targetOffset, final String targetKey, final String cacheId, final boolean keep, final Dimension scale, final byte priority, final Image placeholderImage, final boolean maintainAspectRatio) {
    if (Display.getInstance().isEdt()) {
        Display.getInstance().scheduleBackgroundTask(new Runnable() {

            public void run() {
                createImageToStorage(url, targetList, targetModel, targetOffset, targetKey, cacheId, keep, scale, priority, placeholderImage, maintainAspectRatio);
            }
        });
        return;
    }
    Image im = cacheImage(cacheId, keep, null, scale, placeholderImage, maintainAspectRatio);
    ImageDownloadService i = new ImageDownloadService(url, targetList, targetOffset, targetKey);
    i.targetModel = targetModel;
    i.maintainAspectRatio = maintainAspectRatio;
    if (im != null) {
        i.setEntryInListModel(targetOffset, im);
        targetList.repaint();
        return;
    }
    // image not found on cache go and download from the url
    i.cacheImages = true;
    i.cacheId = cacheId;
    i.keep = keep;
    i.toScale = scale;
    i.placeholder = placeholderImage;
    i.setPriority(priority);
    i.setFailSilently(true);
    NetworkManager.getInstance().addToQueue(i);
}
Also used : EncodedImage(com.codename1.ui.EncodedImage) Image(com.codename1.ui.Image) FileEncodedImage(com.codename1.components.FileEncodedImage) StorageImage(com.codename1.components.StorageImage)

Aggregations

FileEncodedImage (com.codename1.components.FileEncodedImage)5 StorageImage (com.codename1.components.StorageImage)5 EncodedImage (com.codename1.ui.EncodedImage)5 Image (com.codename1.ui.Image)5 NetworkEvent (com.codename1.io.NetworkEvent)2 ImageDownloadService (com.codename1.io.services.ImageDownloadService)2 WebBrowser (com.codename1.components.WebBrowser)1 ConnectionRequest (com.codename1.io.ConnectionRequest)1 Form (com.codename1.ui.Form)1 ActionEvent (com.codename1.ui.events.ActionEvent)1 ActionListener (com.codename1.ui.events.ActionListener)1 BrowserNavigationCallback (com.codename1.ui.events.BrowserNavigationCallback)1