DougW
DougW

Reputation: 30025

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 writing a Service? That seems a little heavy-weight. Are there any solutions for this already out there?

Upvotes: 0

Views: 1560

Answers (2)

Nathan Schwermann
Nathan Schwermann

Reputation: 31493

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

Upvotes: 1

Romain Guy
Romain Guy

Reputation: 98501

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.

Upvotes: 2

Related Questions