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="http://schemas.android.com/apk/res/android"
    android:id="@+id/imagrParent"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center_vertical|center_horizontal"
        android:contentDescription="@string/app_name"
        android:scaleType="fitXY" >
    </ImageView>

    <ProgressBar
        android:id="@+id/bar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:visibility="visible" />

</RelativeLayout>

Step 3) Now set AhamdDownloaderManager to load images


  ImageView img = (ImageView) findViewById(R.id.imageView);
  ProgressBar bar = (ProgressBar) findViewById(R.id.bar);
  img.setTag(Url_To_download);
  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


 @Override
 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


ahmad.setCustomURL("www.google.com");

Download Sample Project

6 comments:

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

    ReplyDelete
  2. thank you for your nice code ;)

    ReplyDelete
  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.

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

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

    String thumb_url = cursor.getString(cursor
    .getColumnIndex(DBConstant.COLUMN_STORE_THUMBNAIL));

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


    }

    Please let me know how to fix this.

    ReplyDelete
    Replies
    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, adapter.bar, this);

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

      And now use these line in Bind View method
      adapter.thumbnail.setTag(thumb_url);
      ahmadDownload.DisplayImage(thumb_url , mContext, adapter.thumbnail, adapter.bar, 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.

      Delete
    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.

      Delete
    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.

      Delete

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