Class LazyImageLoader


  • public class LazyImageLoader
    extends Object
    An instance of this class can be used to load images from a given URL as Bitmaps lazily and apply them to a given ImageView once loaded.

    The image loader offers in-memory caching for bitmaps as well as local file caching for downloaded content.

    This implementation manages its own executor thread and if you use it, make sure you shut down the image loader using the shutdown() method wither when all your resources are loaded, or, once the parent activity is destroyed. For example, in your activity use the image loader like this:

    Note that the API needs WRITE_EXTERNAL_STORAGE permission which should be added and handled by the caller

    private LazyImageLoader imageLoader; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.imageLoader = new LazyImageLoader(this); } protected void onDestroy() { super.onDestroy(); this.imageLoader.shutdown(); }

    Since:
    1.0.0
    • Constructor Detail

      • LazyImageLoader

        public LazyImageLoader​(@NonNull
                               Context context)
        Create a new lazy image loader for a given context
        Parameters:
        context - the context
      • LazyImageLoader

        public LazyImageLoader​(@NonNull
                               Context context,
                               @DrawableRes
                               int defaultImg)
        Create a new lazy image loader for a give context with a default image id that is applied to all image views
        Parameters:
        context - the context
        defaultImg - resource id of a default image
      • LazyImageLoader

        public LazyImageLoader​(@NonNull
                               Context context,
                               @Nullable
                               String sdcardDirName,
                               @DrawableRes
                               int defaultImg)
        Create a new lazy image loader for a give context with a default image id that is applied to all image views. This image loader uses a sub directory created under the Environment.getExternalStorageDirectory() if its available.
        Parameters:
        context - teh context
        sdcardDirName - the sub folder name under Environment.getExternalStorageDirectory() used as cache folder
        defaultImg - the default image resource id
    • Method Detail

      • loadImage

        public void loadImage​(@NonNull
                              String url,
                              @NonNull
                              ImageView imageView,
                              int targetWidth)
        Asynchronously download the image from the given image URL and display the resulting bitmap on the corresponding ImageView.
        Parameters:
        url - Image download url
        imageView - View which will render the image
        targetWidth - If width is different from -1 the image will be scaled down while decoding. In-app memory will be saved
        Since:
        1.2.0
      • loadImage

        public void loadImage​(@NonNull
                              String url,
                              @NonNull
                              String name,
                              @NonNull
                              ImageView imageView,
                              int targetWidth)
        Asynchronously download the image from the given image URL and display the resulting bitmap on the corresponding ImageView.
        Parameters:
        url - Image download url
        name - Image key name for cache
        imageView - View which will render the image
        targetWidth - If width is different from -1 the image will be scaled down while decoding. In-app memory will be saved
        Since:
        1.2.0
      • setPlaceHolder

        public void setPlaceHolder​(@NonNull
                                   ImageView imageView)
        Apply the configured placeholder drawable to the given image view.
        Parameters:
        imageView - the image view
      • shutdown

        public void shutdown()
        Shut down this image loader. You will not be able to use it anymore after this method was called.
        Since:
        1.2.0
      • clearCache

        public void clearCache()
        Clears the in-memory bitmap cache and removed all cached files.