开发者

Android - Threaded BitmapFactory image decoding

开发者 https://www.devze.com 2023-01-20 00:20 出处:网络
Our Android app does a lot of image decoding.We fetch a lot of images from the internet, local storage caches, etc.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消