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