Android cache image from url

In this Android tip, I am going to show you how to cache an image from url so that the image can be displayed quickly without internet connection. In a chat application, typically, profile pictures are downloaded at the first time and cached in memory or disc to load quickly in an ImageView. A popular library that I always use in my projects to achieve the goal is Universal Image Loader. It can download and cache multiple images asynchronously. There are many configuration options of an ImageLoader instance to meet your requirements.

Now to have an example application on caching image from url using Universal Image Loader, you create a new project in Android Studio. Download the library jar file from the link above and add it to the libs directory of your project. Do not forget to make it as library (Add As Library). In the activity_main.xml file, you add an ImagView to display the image.

     android:layout_centerHorizontal="true" />

Here is the code written in the MainActivity class to download the image from the url, cache it on memory and disc. Before using an instance of ImageLoader to download and cache the image, you need to configure the image loader by creating an instance of ImageLoaderConfiguration class. With the instance, you can specify configuration options such as thread priority, tasks processing order, maximum sizes to be cached on memory and disc, log messages, etc. By using the DisplayImageOptions class, you can specify options (e.g. caching on memory, displaying an image while the image from url is loading) for the loaded image.

public class MainActivity extends ActionBarActivity {

   protected void onCreate(Bundle savedInstanceState) {
     ImageView imageView = (ImageView) findViewById(;

     // Configure image loader
     ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
           // Thread priority
                // Deny cache multiple image sizes on memory
                // Processing order like a stack (last in, first out)
                // Max image size to cache on memory
                // Max image size to cache on disc
                // Write log messages

     // Get ImageLoader instance
     ImageLoader imageLoader=ImageLoader.getInstance();
     // Define image display options

     DisplayImageOptions options = new DisplayImageOptions.Builder()
           // Cache loaded image in memory and disc
           // Show Android icon while loading
     String imgUrl="";
     imageLoader.displayImage(imgUrl, imageView, options);


Before you run the example application, do not forget to declare INTERNET and WRITE_EXTERNAL_STORAGE permissions in AndroidManifest.xml file.


Posted by: Dara | post date: 03-07-2015 | Subject: Android Apps Development

write programming tip

This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.