Tutorials Android Universal Image Loader Library in Android

Universal Image Loader Library in Android

In this example we’ll show you how to use Universal Image Loader library in your android project.

What is Universal Image Loader?

Universal Image Loader is an smart and powerful library that helps in loading, caching and displaying images on Android. This means, using this library you can download remote images and display on ImageView.

Universal Image Loader Features

  • Asynchronous and multi-threaded image loading. This allows you to download multiple images Asynchronously.
  • Supports various configurations that helps to tune for your requirement. With this you can control memory, cache type, decoder, display image options, etc.
  • Possibility of image caching in memory and/or on device’s file system (or SD card)
  • Possibility to “listen” loading process. Allows various callback methods using which you will get to know the progress/state of your download request.

UniversalImageLoader

Integrating Universal Image Loader in Android

Integrating this library is quite easy. Here we’ll show you steps to download and integrate this library in Android application.

1. Download Universal Image Loader

Download Universal Image Loader JAR and put the JAR in the libs folder of your Android project. You can also fork the library on GitHub

2. Mainfest permissions

Add below required permission in your application Manifest file.

<manifest>
    <uses-permission android:name="android.permission.INTERNET" />
    <!-- Include next permission if you want to allow UIL to cache images on SD card -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    ...
    <application android:name="MyApplication">
        ...
    </application>
</manifest>

3. Library setup in your Application class

import android.app.Application;
import com.nostra13.universalimageloader.cache.memory.impl.WeakMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;

public class MyApplication extends Application {
	@Override
	public void onCreate() {
		super.onCreate();

		// UNIVERSAL IMAGE LOADER SETUP
		DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
				.cacheOnDisc(true).cacheInMemory(true)
				.imageScaleType(ImageScaleType.EXACTLY)
				.displayer(new FadeInBitmapDisplayer(300)).build();

		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
				getApplicationContext())
				.defaultDisplayImageOptions(defaultOptions)
				.memoryCache(new WeakMemoryCache())
				.discCacheSize(100 * 1024 * 1024).build();

		ImageLoader.getInstance().init(config);
		// END - UNIVERSAL IMAGE LOADER SETUP
	}
}

4. Download and display bitmap on ImageView

//your image url
String url = "http://stacktips.com/wp-content/uploads/2014/05/UniversalImageLoader-620x405.png";

ImageLoader imageLoader = ImageLoader.getInstance();
DisplayImageOptions options = new DisplayImageOptions.Builder().cacheInMemory(true)
				.cacheOnDisc(true).resetViewBeforeLoading(true)
				.showImageForEmptyUri(fallback)
				.showImageOnFail(fallback)
				.showImageOnLoading(fallback).build();
		
//initialize image view
ImageView imageView = (ImageView) findViewById(R.id.imageView1)		

//download and display image from url
imageLoader.displayImage(url, imageView, options);
Total
0
Shares
guest
20 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Phillip
Phillip
8 years ago

Thanx I’ll try it up

mahadev dalvi
mahadev dalvi
8 years ago

thanks

Nayana Bhoj
Nayana Bhoj
7 years ago

getting error as fallback cannot be resolved to a variable???

Rohit Yeole
Rohit Yeole
7 years ago

Thanx buddy ! Really useful tutorial ! :like:

JavaTechig
JavaTechig
7 years ago

uri to image resource. It can be drawable.

hossein kurd
hossein kurd
7 years ago

when i want to show huge images there is out of memory error , how to resolve loading huge images?

JavaTechig
JavaTechig
7 years ago
Reply to  hossein kurd

What is the approx image size. ??

JavaTechig
JavaTechig
7 years ago

you probably need to run profiler and see the memory consumption. 2250 sized multiple images can be a threat if not handled properly.

hossein kurd
hossein kurd
7 years ago

There is no way elsr using thumb image.

Nilanchala Panigrahy
Nilanchala Panigrahy
7 years ago

UIL used to download images form server. For local images you can use the following method as per your requirements.

img.setBackgroundResource();
img.setImageBitmap();
img.setBackground();

Dhruvit Darji
Dhruvit Darji
7 years ago

yes i know this default method but i want to set round corner image provided by image loader..

Kian
Kian
7 years ago

How to add text for each images

Hammayun Raza
Hammayun Raza
7 years ago

How to create listview with image and textview using universal image loader library ?Any one help me plz

Nilanchala Panigrahy
Nilanchala Panigrahy
7 years ago
Reply to  Hammayun Raza
Omar Hossam Eldin
Omar Hossam Eldin
7 years ago

Why do you reuse the DisplayImageOptions, while you can call displayImage, without using the 3rd parameters and this used the default one written in the Application class.

Amr Zagloul
Amr Zagloul
7 years ago

Hello Omar, He need to customise the failure and empty url for this view only not over all view that using Image loader over the app 🙂

tapan
tapan
6 years ago

but what for multiple images ?

Kian Kiani
Kian Kiani
5 years ago

How I can create several galleries using universal image loader?

Gaurav
Gaurav
5 years ago

discCacheSize is changed to diskCacheSize & cacheOnDisc changed to cacheOnDisk if you are getting deprecated error.

Nilanchala Panigrahy
Nilanchala Panigrahy
5 years ago
Reply to  Gaurav

Thanks Gaurav.

Previous Post
Javatechig mobile application on Google Play

Javatechig Mobile App on Google Play

Next Post

WWW Client Servers Architecture

Related Posts
By clicking “Allow All”, you agree to the storing of cookies on your device to enhance site navigation, analyze site usage, and assist in our marketing efforts. Cookie Notice
Allow All
20
0
Would love your thoughts, please comment.x
()
x