Introduction
This article explains the Universal Image Loader. The Universal Image Loader allows you to set various image view options. You can load the image from a URL at runtime using this. The advantage of using this is that you need not store all the images you want to use in your project (in drawable). This saves memory. In large projects where memory management is crucial, using Universal Image Loader is a good idea.
Step 1
You will need to include "universal-image-loader-1.7.0.jar" from the "library" zip, provided above, as a library in your project. Copy the jar file provided to the clipboard and paste it on the "libs" in your Eclipse project.
This jar contains the main functionalities that we will be using.
Step 2
First, we will make an application class that will set the basic configurations.
Right-click on your package name then select "New" -> "Class". Name it "App" and add the following code to it:
- package com.chhavi.universalimageloader;
- import android.app.Application;
- import android.content.Context;
- import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
- import com.nostra13.universalimageloader.core.ImageLoader;
- import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
- import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
- public class App extends Application {
- @Override
- public void onCreate() {
- super.onCreate();
- initImageLoader(getApplicationContext());
- }
- public static void initImageLoader(Context context) {
- ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
- .threadPriority(Thread.NORM_PRIORITY - 2)
- .denyCacheImageMultipleSizesInMemory()
- .discCacheFileNameGenerator(new Md5FileNameGenerator())
- .tasksProcessingOrder(QueueProcessingType.LIFO)
- .enableLogging()
- .build();
- ImageLoader.getInstance().init(config);
- }
- }
Step 3
Open the "activity_main" layout file and add the following code to it:
- <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent" >
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="center"
- android:paddingBottom="10dip"
- android:paddingTop="20dip"
- android:text="Welcome to Universal Image Loader...."
- android:textSize="24sp" />
- <Button
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_margin="10dip"
- android:onClick="listFunc"
- android:text="Click here" />
- </LinearLayout>
- </ScrollView>
Step 4
Create a layout for displaying the list of images in a list view. Right-click on layout then select "New" -> "Other" -> "Android" -> "Android Layout XML file". Name this file as "image_listview_layout" and add the following code to it:
- <ListView xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@android:id/list"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent" />
Step 5
Right-click on layout then select "New" -> "Other" -> "Android" -> "Android Layout XML file". Name this file as "image_list_layout" and add the following code to it:
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content" >
- <ImageView
- android:id="@+id/image"
- android:layout_width="85dip"
- android:layout_height="80dip"
- android:layout_margin="3dip"
- android:adjustViewBounds="true"
- android:scaleType="centerCrop" />
- <TextView
- android:id="@+id/text"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_gravity="left|center_vertical"
- android:layout_marginLeft="20dip"
- android:textSize="22sp" />
- </LinearLayout>
Step 6
For providing a larger view on the image when the user selects that image from the list.
Right-click on layout then select "New" -> "Other" -> "Android" -> "Android Layout XML file". Name this file as "view_layout" and add the following code to it:
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <ImageView
- android:layout_width="300dp"
- android:layout_height="300dp"
- android:id="@+id/imFinal"
- android:layout_marginLeft="20dp"
- android:layout_marginRight="20dp"
- android:layout_marginTop="40dp"/>
- </LinearLayout>
Step 7
Open "MainActivity" and add the following code to it:
- package com.chhavi.universalimageloader;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.View;
- public class MainActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- public void listFunc(View view) {
- Intent i = new Intent(this, ListOfImages.class);
- startActivity(i);
- }
- }
Step 8
The following is for displaying the image list.
Right-click on your package name then select "New" -> "Class". Name it "ListOfImages" and add the following code to it:






?????? ???????Posted Jul 18, 2014, 8:28 AM
im getting an error on .enableLogging()
Sivaramakrishna JPosted Dec 9, 2013, 12:55 AM
i got thumbnail of images and videos file then how to display these thumbnail with this lib in grid view?
Chhavi GoelPosted Jul 8, 2013, 10:37 PM
this means that you have not added the required libraries....add the libraries provided in "library.rar" above
lal pPosted Jul 8, 2013, 3:34 AM
it give error like "cannot resolve nostra13"
lal pPosted Jul 8, 2013, 3:34 AM
import com.nostra13.universalimageloader.core.DisplayImageOptions; import com.nostra13.universalimageloader.core.ImageLoader; import com.nostra13.universalimageloader.core.assist.ImageLoadingListener; import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener; import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;