Android - Threaded BitmapFactory image decoding
Our Android app does a lot of image decoding. We fetch a lot of images from the internet, local storage caches, etc.
Up to now, these images are being decoded on the UI thread (using BitmapFactory.decodeX() methods). It's been causing some timeout crashes because the UI doesn't respond quickly enough to user input.
I could write a little AsyncTask that encapsulates decoding, but I'm not sure that's a good idea. Spawning threads is expensive, and that would be spawning and tearing down a ton of them.
So what's the best way to put this on another thread? Do I need to go to the extent of wr开发者_StackOverflow中文版iting a Service? That seems a little heavy-weight. Are there any solutions for this already out there?
AsyncTask keeps a pool of threads, so you don't pay much cost for using several instances of AsyncTasks. Either use AsyncTask or use a single thread with a request queue to decode the images.
You can use an IntentService
to que up your download/decodes just call startService()
for each image all at once and it will do them one at a time. then you can pass it back to your activity with a ResultReceiver
精彩评论