0

I want to download multiple images using ImageLoader.loadImage which will launch multiple threads. Because they take a while to execute and i don't want to lock up the UI, i want to run them in the doInBackground() function of AsyncTask.

However I cannot launch new threads in the doInBackground() function. Is there a way around this?

Simon Dorociak
  • 33,374
  • 10
  • 68
  • 106
Jake
  • 2,877
  • 8
  • 43
  • 62
  • why wouldn't you be able to launch new threads? Any errors? – Goddchen Apr 01 '13 at 18:43
  • 3
    You might want to read up on Threads and AsyncTask a bit. You're right in wanting to use an AsyncTask in order not to lock up the UI -- as of Android 4.x that's actually a REQUIREMENT and your app will crash if you attempt to access the internet on the UI thread. So far, so good. Since `doInBackground` already runs off he UI thread, there's really no reason to spin off additional threads. If you do want to run multiple threads to run multiple downloads concurrently, then you don't need AsyncTask... – 323go Apr 01 '13 at 18:44
  • @Jake check the edited answer. Once you have urls , you can display images in listview or gridview without blocking the UI using Universal Image Loader. – Raghunandan Apr 01 '13 at 19:38

1 Answers1

1

I agree with the comment made by 323go

AsyncTask is designed to be a helper class around Thread and Handler and does not constitute a generic threading framework. AsyncTasks should ideally be used for short operations (a few seconds at the most.) If you need to keep threads running for long periods of time, it is highly recommended you use the various APIs provided by the java.util.concurrent pacakge such as Executor, ThreadPoolExecutor and FutureTask. (Straight from the doc)

As an alternative you can use https://github.com/octo-online/robospice. You can make multiple spice request's.

Universal Image Loader

To download and display large number of images, use a list view or grid view. For this you can use Universal Image Loader or Lazy list. Universal Image loader works on the sample principle as lazy list.

I had to display images from picasa album public folder (about 300 - 500). I made a http request and the response was json. I used asynctask to post http request, get response , parse json to get the urls. Once i got the url's i used Universal Image Loader to load images. So you can use asynctask for a short running operation.

Suppose you can view 3 images at a time in a list. The three images are downloaded, cached if its not and displayed. When you scroll the procedure repeats. Once cached images need not be downloaded again. The UI in this case is not blocked. You can scroll down any time.

Url is considered as the key. Image is cached to sdcard or phone memory. The location for cache can be specified. If the image exists in cache. display images from cache, if not download, cache and display images.

Both use caching. Universal Image Loader has lot of configuration options. https://github.com/nostra13/Android-Universal-Image-Loader

Look at the features in the link.

In your custom adapter constructor

 File cacheDir = StorageUtils.getOwnCacheDirectory(a, "your folder");

 // Get singletone instance of ImageLoader
 imageLoader = ImageLoader.getInstance();
 // Create configuration for ImageLoader (all options are optional)
 ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(a)
      // You can pass your own memory cache implementation
     .discCache(new UnlimitedDiscCache(cacheDir)) // You can pass your own disc cache implementation
     .discCacheFileNameGenerator(new HashCodeFileNameGenerator())
     .enableLogging()
     .build();
  // Initialize ImageLoader with created configuration. Do it once.
  imageLoader.init(config);
  options = new DisplayImageOptions.Builder()
  .showStubImage(R.drawable.stub_id)//display stub image
  .cacheInMemory()
  .cacheOnDisc()
  .displayer(new RoundedBitmapDisplayer(20))
  .build();

In your getView()

  ImageView image=(ImageView)vi.findViewById(R.id.imageview); 
   imageLoader.displayImage(imageurl, image,options);//provide imageurl, imageview and options

You can configure with other options to suit your needs.

You should use a viewholder for smooth scrolling and performance. http://developer.android.com/training/improving-layouts/smooth-scrolling.html

Raghunandan
  • 132,755
  • 26
  • 225
  • 256