Search in sources :

Example 1 with URLImage

use of com.codename1.ui.URLImage in project CodenameOne by codenameone.

the class CodenameOneImplementation method downloadImageToCache.

/**
 * Downloads an image from a URL to the cache. Platforms
 * that support a native image cache {@link #supportsNativeImageCache() } (e.g. Javascript) override this method to defer to the
 * platform's handling of cached images.  Platforms that have a caches directory ({@link FileSystemStorage#hasCachesDir() }
 * will use that directory to cache the image.  Other platforms will just download to storage.
 *
 * @param url The URL of the image to download.
 * @param onSuccess Callback on success.
 * @param onFail Callback on fail.
 *
 * @see URLImage#createToCache(com.codename1.ui.EncodedImage, java.lang.String, com.codename1.ui.URLImage.ImageAdapter)
 */
public void downloadImageToCache(String url, SuccessCallback<Image> onSuccess, final FailureCallback<Image> onFail) {
    FileSystemStorage fs = FileSystemStorage.getInstance();
    if (fs.hasCachesDir()) {
        String name = "cn1_image_cache[" + url + "]";
        name = StringUtil.replaceAll(name, "/", "_");
        name = StringUtil.replaceAll(name, "\\", "_");
        name = StringUtil.replaceAll(name, "%", "_");
        name = StringUtil.replaceAll(name, "?", "_");
        name = StringUtil.replaceAll(name, "*", "_");
        name = StringUtil.replaceAll(name, ":", "_");
        name = StringUtil.replaceAll(name, "=", "_");
        String filePath = fs.getCachesDir() + fs.getFileSystemSeparator() + name;
        // We use Util.downloadImageToFileSystem rather than CodenameOneImplementation.downloadImageToFileSystem
        // because we want it to try to load from file system first.
        Util.downloadImageToFileSystem(url, filePath, onSuccess, onFail);
    } else {
        // We use Util.downloadImageToStorage rather than CodenameOneImplementation.downloadImageToStorage
        // because we want it to try to load from storage first.
        Util.downloadImageToStorage(url, "cn1_image_cache[" + url + "]", onSuccess, onFail);
    }
}
Also used : FileSystemStorage(com.codename1.io.FileSystemStorage)

Example 2 with URLImage

use of com.codename1.ui.URLImage in project CodenameOne by codenameone.

the class GenericListCellRenderer method updateModelValues.

private Object updateModelValues(Map h, String currentName, Component[] entries, int iter, Object val) {
    String uiid = (String) h.get(currentName + "_uiid");
    if (uiid != null) {
        entries[iter].setUIID(uiid);
    }
    if (currentName.endsWith("_URLImage")) {
        URLImage img = (URLImage) h.get(currentName + "Actual");
        if (img != null) {
            val = img;
        } else {
            String name = (String) h.get(currentName + "Name");
            if (name == null) {
                name = val.toString();
                name = name.substring(name.lastIndexOf('/'));
            }
            val = URLImage.createToStorage(placeholders.get(currentName), name, val.toString(), adapter);
            h.put(currentName + "Actual", val);
        }
    }
    return val;
}
Also used : URLImage(com.codename1.ui.URLImage)

Example 3 with URLImage

use of com.codename1.ui.URLImage in project CodenameOne by codenameone.

the class URLImage method createCachedImage.

/**
 * Creates an image that will be downloaded on the fly as necessary.  On platforms that support a native
 * image cache (e.g. Javascript), the image will be loaded directly from the native cache (i.e. it defers to the
 * platform to handle all caching considerations.  On platforms that don't have a native image cache but
 * do have a caches directory {@link FileSystemStorage#hasCachesDir()}, this will call {@link #createToFileSystem(com.codename1.ui.EncodedImage, java.lang.String, java.lang.String, com.codename1.ui.URLImage.ImageAdapter) }
 * with a file location in the caches directory.  In all other cases, this will call {@link #createToStorage(com.codename1.ui.EncodedImage, java.lang.String, java.lang.String) }.
 *
 * @param imageName The name of the image.
 * @param url the URL from which the image is fetched
 * @param placeholder the image placeholder is shown as the image is loading/downloading
 * and serves as the guideline to the size of the downloaded image.
 * @param resizeRule One of {@link #FLAG_RESIZE_FAIL}, {@link #FLAG_RESIZE_SCALE}, or {@link #FLAG_RESIZE_SCALE_TO_FILL}.
 * @return a Image that will initially just delegate to the placeholder
 */
public static Image createCachedImage(String imageName, String url, Image placeholder, int resizeRule) {
    if (Display.getInstance().supportsNativeImageCache()) {
        CachedImage im = new CachedImage(placeholder, url, resizeRule);
        im.setImageName(imageName);
        return im;
    } else {
        ImageAdapter adapter = null;
        switch(resizeRule) {
            case FLAG_RESIZE_FAIL:
                adapter = RESIZE_FAIL;
                break;
            case FLAG_RESIZE_SCALE:
                adapter = RESIZE_SCALE;
                break;
            case FLAG_RESIZE_SCALE_TO_FILL:
                adapter = RESIZE_SCALE_TO_FILL;
                break;
            default:
                adapter = RESIZE_SCALE_TO_FILL;
                break;
        }
        FileSystemStorage fs = FileSystemStorage.getInstance();
        if (fs.hasCachesDir()) {
            String name = "cn1_image_cache[" + url + "]";
            name = StringUtil.replaceAll(name, "/", "_");
            name = StringUtil.replaceAll(name, "\\", "_");
            name = StringUtil.replaceAll(name, "%", "_");
            name = StringUtil.replaceAll(name, "?", "_");
            name = StringUtil.replaceAll(name, "*", "_");
            name = StringUtil.replaceAll(name, ":", "_");
            name = StringUtil.replaceAll(name, "=", "_");
            String filePath = fs.getCachesDir() + fs.getFileSystemSeparator() + name;
            // System.out.println("Creating to file system "+filePath);
            URLImage im = createToFileSystem(EncodedImage.createFromImage(placeholder, false), filePath, url, adapter);
            im.setImageName(imageName);
            return im;
        } else {
            // System.out.println("Creating to storage ");
            URLImage im = createToStorage(EncodedImage.createFromImage(placeholder, false), "cn1_image_cache[" + url + "@" + placeholder.getWidth() + "x" + placeholder.getHeight(), url, adapter);
            im.setImageName(imageName);
            return im;
        }
    }
}
Also used : FileSystemStorage(com.codename1.io.FileSystemStorage)

Aggregations

FileSystemStorage (com.codename1.io.FileSystemStorage)2 URLImage (com.codename1.ui.URLImage)1