How to execute other threads from doinbackground()

瘦欲@ 提交于 2019-12-08 13:15:04

问题


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?


回答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



来源:https://stackoverflow.com/questions/15749454/how-to-execute-other-threads-from-doinbackground

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!