-3

i am downloading (pref-etch) images from server at some time of interval

so i wanted to know what is the best way to download images

asyntask

service

intentservice

please help

Mahtab
  • 269
  • 3
  • 11

1 Answers1

1

Case 1: If you have to download very few images (like one or two in an activity) from Internet, then use Asynctask .

Case 2: If you have to download many images specially for ListView items , you must have to implement it in a dedicated background thread by using HandlerThread, Handler , message and Looper in Android. Avoid using a service here. Also, you have to implement image caching as you dont want to download the same image multiple times in a session.

Advice: If the second is your case, don't implement the above by yourself. Save your time and use some awesome, efficient and fast libraries used by millions of Android apps.

You can use:

  • Picasso by Square
  • Glide
  • Volley

Edit: How to use Picasso for downloading images.

All the above mentioned libraries can do that beautifully. I am sharing the solution with Picasso.

    Picasso.with(mContext).load("url").into(new Target() {
        @Override
        public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom loadedFrom) {
         //save the  bitmap into permanent storage or do whatever you want like showing in some ImageView etc.

        }

        @Override
        public void onBitmapFailed(Drawable drawable) {

        }

        @Override
        public void onPrepareLoad(Drawable drawable) {

        }
    });

Hope this helps. If still have any issue , please feel free to comment.

Mahendra Chhimwal
  • 1,810
  • 5
  • 21
  • 33
  • i have to download more images and the image size will be large basically i wanted to pref etch images and these libraries not pref etch images – Mahtab May 06 '16 at 10:49
  • what do you mean by pre fetch image? Do you mean download them, without showing in ImageView at a time of downloading? – Mahendra Chhimwal May 06 '16 at 17:07
  • if i download the data from service using picasso or any other library so is it possible to download ? i am asking this question because i heard that image loader must be invoked from main thread... – Mahtab May 09 '16 at 17:14
  • You can do that too. Please check the question here http://stackoverflow.com/questions/27239203/synchronous-image-loading-on-a-background-thread-with-picasso-without-get – Mahendra Chhimwal May 10 '16 at 06:06