Search This Blog

Wednesday 10 October 2012

Android : Lazy loading and Caching of Images with Image downloader

Downloading does always fascinated me. When we download movie, song or Wallpaper. But when i went into development then i came to know how much complication involve in that.
I saw so many people struggling in downloading. So i decide to make one download manager for all android developer. And now i am revealing all my hard work to you guys so that it make your work much easier.

When to use AhamdDownloaderManager :->

   when you want to download images
  •  When you want to cache images for later use
  • When you want to handle network failure and other error.

When not to use AhamdDownloaderManager :->

If you want to download Video, Mp3 then its not for you guys

Qualities of AhamdDownloaderManager :->

  • It will check automatically your sdcard is mounted or not and give you alert
  • Return a callback with Bitmap value as null if in any case Image is not successfully downloaded
  • Save all image inside sdcard and later on it use these image if URL name is same.
  •  Fast

How to use AhamdDownloaderManager :->

Step 1) Download Jar and add to your project. Download this Jar and to your project
Step 2) It handle progress untill your images downloaded so create your layout similar like this

<RelativeLayout xmlns:android=""
    android:layout_height="fill_parent" >

        android:scaleType="fitXY" >

        android:visibility="visible" />


Step 3) Now set AhamdDownloaderManager to load images

  ImageView img = (ImageView) findViewById(;
  ProgressBar bar = (ProgressBar) findViewById(;
  AhmadDownloadManager ahmadDownload = new AhmadDownloadManager(this);
  ahmadDownload.DisplayImage(Url_To_download, this, img, bar, this);

Step 4) Handle failure : - We have one call back interface that we have to implement in Step 3

 public void result(Bitmap bitmap) {
  Log.i("AhmadDownloadCallback", "Not able to download");


Update 1.0.1 : I updated it on demand of my reader for reason likes.

1) How to scale image on base of custom height and width : New download jar have method 

ahmad.setCustomHeight(100, 100);


2) Avoiding condition if your URL is null : set Custom URL in new jar


Download Sample Project


  1. Thank you Sarabjeet, I expect you keep visiting and never forget to leave your feedback

  2. thank you for your nice code ;)

  3. Hi,

    I tried your code but I am facing some issue while using the same in Cursor Adapter. I tried to load list of some 700 image. Images Fetching from database. SO i used Cursor adapter.
    i tried like this . But in this case Image is not loading.

    At the same time I tried to cut and paste below line to constructor
    "AhmadDownloadManager ahmadDownload = new AhmadDownloadManager(mContext);"In this case Constructor image is loading . But its showing wrong image.

    public void bindView(View convertView, final Context context, Cursor cursor) {

    final ViewHolder adapter;
    adapter = (ViewHolder) convertView.getTag();
    String storename = cursor.getString(cursor

    String thumb_url = cursor.getString(cursor

    AhmadDownloadManager ahmadDownload = new AhmadDownloadManager(mContext);
    ahmadDownload.DisplayImage(thumb_url , mContext, adapter.thumbnail,, this);


    Please let me know how to fix this.

    1. You are not setting tag to your ImageView that's why it shows wrong image. Change these two line like --
      ahmadDownload.DisplayImage(thumb_url , mContext, adapter.thumbnail,, this);

      Use this line in constructor
      AhmadDownloadManager ahmadDownload = new AhmadDownloadManager(mContext);

      And now use these line in Bind View method
      ahmadDownload.DisplayImage(thumb_url , mContext, adapter.thumbnail,, this);

      Most importantly archana, you are not checking correct condition so that your adapter recycle view. Make Holder object only if you convertview value is null.
      Please contact on my google plus profile if you want to post any piece of code.

    2. Hi Ahmad,

      Thanks for your response. As you said I missed above lines. Now its working fine. I have one more doubt. In case my url is null i would like to set some static image. How to do that.

      I put some condition but now working. Can u please share me your mail id.

    3. I am glad that you solved issue.I am sorry,can not share my email here.Stay in touch from my google profile. And for your null url profile, you can keep if condition before you add this to ahmaddownloadmanger.


Feedback always help in improvement. If you have any query suggestion feel free to comment and Keep visiting my blog to encourage me to blogging

Android News and source code