Home / Basics / Android Universal Image Loader Tutorial – Library #2

Android Universal Image Loader Tutorial – Library #2

This is library #2 in this series 100 Library in Android , in the previous article we had Android Studio Plugin Drawable Importer. In this tutorial we are having the Android Universal Image Loader Library which is Android library #1 on GitHub. It aims to provide a powerful, flexible and highly customizable instrument for image loading, caching and displaying. It provides a lot of low and high configuration options and good control over the image loading and caching process.

Library Features:

  • Multithread image loading (async or sync)
  • Wide customization of ImageLoader’s configuration (thread executors, downloader, decoder, memory and disk cache, display image options, etc.)
  • Many customization options for every display image call (stub images, caching switch, decoding options, Bitmap processing and displaying, etc.)
  • Image caching in memory and/or on disk (device’s file system or SD card)
  • Listening loading process (including downloading progress)

 Our App:

Our app will use Android Universal Image Loader to load an avatar image, we will have an Image Loading Listener which is one of the main features of the plugin so you can track your loading when it begins and when it fails and so on. While the image is loading it will display a placeholder image, if loading failed it will display fail image as shown below and you can download these images from this link
Android Universal Image Loader

Main Steps: 

1. Install the library.

2. Add permissions to your manifest file.

3. Make a global ImageLoaderConfiguration object and initialize ImageLoader with these configs.

4.  Load image using ImageLoader object, before doing that we will make an object of DisplayImageOptions to specify options like showImageOnLoading and showImageOnFail and OnEmptyURLImage, also we can specify if we need to cache our image on memory or in disk or both.

How the caching work ?

Android Universal Image Loader enables developers to cache the image on the memory or on the disk (device storage), the flow of loading images is as shown in this illustration, the image is cached on the device storage first then on memory.

Android Universal Image Loader

Android Universal Image Loader

1. Creating Android Project, In Android Studio, create a new project by navigating to File ⇒ New Project and fill all the required details. When it prompts to select a default activity, select Empty Activity and proceed.

2. Install the library using Gradle dependency, open Application build.gradle file and add the following code under dependencies:

3. Android Universal Image Loader library uses Internet and device storage, so we should add these permissions in the manifest file as following code:

4. Open the layout file of the activity activity_main.xml and add below code. This includes just an image view which we load our target image on it.

5. Now lets make a global configuration object and initialize ImageLoader with this configs, In the main activity we will do these steps, open MainActivity.java and add this code in your onCreate method.

You can add a lot of configuration options to your config object, such like memory cache size, thread pool size and so on, All options in Configuration builder are optional and Use only those you really want to customize.
Example for the complete config object options:

we are not going deep in these options in this tutorial as the library handles most of them automatically and we don’t have to customize it.

5. Create an object for Image Display Options, display options are local for every display task.

6. Now lets make an object of our image and load it from its URL, and make an object from the ImageLoadingListener as well, so the complete code of the MainActivity.java is as following:

7. Run the project and test it. You should able see The Android Universal Image Loader works and the output should look like this image below, try disabling the network from your device and try to use empty URL and see what’s the results:

Android Universal Image Loader

 

Summary
Article Title
Android Universal Image Loader Tutorial - Library #2
Description
Android Universal Image Loader tutorial with Loading Listener to track your loading While the image is loading and it will display a placeholder image
Author
Organization
Android Gifts

About #AndroidGifts

#AndroidGifts

Check Also

Fresco Android Library Tutorial

Fresco Android Library Tutorial – Library #8

This is library #8 in this series 100 Library in Android , in the previous article we …

Leave a Reply