• Latest Code...

    Featured Post

    Implementing Hilt in a Kotlin Android Jetpack Compose Project with MVVM Architecture

     In modern Android development, maintaining a scalable codebase can be challenging, especially when it comes to dependency management. Hilt,...

    How to lazy load of images in ListView or LazyList?

    Lazy List is lazy loading of images from sd-card or from server using urls. It is like on demand loading images.
    Images can be cached to local sd-card or phone memory. Url is considered the key. If the key is present in sd-card, display images from sd-card else display image by downloading from server and cache the same to location of your choice. The cache limit can set. You can also choose your own location to cache images. Cache can also be cleared.
    Instead of user waiting to download large images and then displaying lazy list, loads images on demand. Since images are cached you can display images offline.
    A simple library to display images in Android ListView. Images are being downloaded asynchronously in the background. Images are being cached on SD card and in memory. Can also be used for GridView and just to display images into an ImageView.
    other examples:
    James A Wilson give the best examles of highest vote on stackover.
    package com.wilson.android.library;


    import java.io.IOException;

    public class DrawableManager {
    private final Map<String, Drawable> drawableMap;

    public DrawableManager() {
    drawableMap
    = new HashMap<String, Drawable>();
    }

    public Drawable fetchDrawable(String urlString) {
    if (drawableMap.containsKey(urlString)) {
    return drawableMap.get(urlString);
    }

    Log.d(this.getClass().getSimpleName(), "image url:" + urlString);
    try {
    InputStream is = fetch(urlString);
    Drawable drawable = Drawable.createFromStream(is, "src");


    if (drawable != null) {
    drawableMap
    .put(urlString, drawable);
    Log.d(this.getClass().getSimpleName(), "got a thumbnail drawable: " + drawable.getBounds() + ", "
    + drawable.getIntrinsicHeight() + "," + drawable.getIntrinsicWidth() + ", "
    + drawable.getMinimumHeight() + "," + drawable.getMinimumWidth());
    } else {
    Log.w(this.getClass().getSimpleName(), "could not get thumbnail");
    }

    return drawable;
    } catch (MalformedURLException e) {
    Log.e(this.getClass().getSimpleName(), "fetchDrawable failed", e);
    return null;
    } catch (IOException e) {
    Log.e(this.getClass().getSimpleName(), "fetchDrawable failed", e);
    return null;
    }
    }

    public void fetchDrawableOnThread(final String urlString, final ImageView imageView) {
    if (drawableMap.containsKey(urlString)) {
    imageView
    .setImageDrawable(drawableMap.get(urlString));
    }

    final Handler handler = new Handler() {
    @Override
    public void handleMessage(Message message) {
    imageView
    .setImageDrawable((Drawable) message.obj);
    }
    };

    Thread thread = new Thread() {
    @Override
    public void run() {
    //TODO : set imageView to a "pending" image
    Drawable drawable = fetchDrawable(urlString);
    Message message = handler.obtainMessage(1, drawable);
    handler
    .sendMessage(message);
    }
    };
    thread
    .start();
    }

    private InputStream fetch(String urlString) throws MalformedURLException, IOException {
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpGet request = new HttpGet(urlString);
    HttpResponse response = httpClient.execute(request);
    return response.getEntity().getContent();
    }
    }
    Alternative of LazyList is:

    open source instrument Universal Image Loader. It is originally based on Fedor Vlasov's project LazyList and has been vastly improved since then.
    • Multithread image loading
    • Possibility of wide tuning ImageLoader's configuration (thread executors, downlaoder, decoder, memory and disc cache, display image options, and others)
    • Possibility of image caching in memory and/or on device's file sysytem (or SD card)
    • Possibility to "listen" loading process
    • Possibility to customize every display image call with separated options
    • Widget support
    • Android 2.0+ support
    Finally, you may use this also,this is also best tutoiral for lazylist image loader.
    I have followed this Android Training and I think it does an excellent job at downloading images without blocking the main UI. It also handles caching and dealing with scrolling through many images: Loading Large Bitmaps Efficiently

    All sources from statckover.

    Happy Coding!!!

    Contact Form

    Name

    Email *

    Message *