从 SD 卡加载大量图像时 AsyncTask 进程缓慢的解决方案

发布于 2024-12-08 20:33:13 字数 2271 浏览 0 评论 0原文

大家好,我有很多图像想要从 SD 卡显示到网格视图。我已按照教程在 is 链接中执行此操作:

http://mihaifonoage.blogspot.com/2009/11/displaying-images-from-sd-card-in.html

一切都很好,除了当图像变成很多,填充它变得非常慢,并且滚动也非常慢。我尝试编辑代码以使用 ViewHolder 方法,但仍然没有成功。

现在,我还使用了在自定义 ListView 中填充图像而不使用 AsyncTask,并且它似乎可以更快地处理它。是的,常识是只使用更快的方法,但首先要澄清一些事情。所以我问:

  1. AsyncTask 在某些情况下是否真的很慢并且不理想,或者因此我使用 Gridview 或 Listview 的事实与它有关?有什么理由吗?我问这个问题是因为 AsyncTask 似乎总是受到很好的青睐。

  2. 任何其他方式、解决方案或技巧可以让我更快地完成此过程?..

注意:我会发布我的代码,但它与链接相同,只是我没有在 MediaStore 查询中使用 getThumbnails() 。提前致谢。

这是我在加载图像时得到的 logcat 输出的部分内容:

10-07 19:42:54.072: DEBUG/dalvikvm(20291): GC_EXPLICIT freed <1K, 48% free 2886K/5511K, external 1574K/14018K, paused 28ms

10-07 19:42:54.092: DEBUG/dalvikvm(20291): GC_EXPLICIT freed <1K, 48% free 2918K/5511K, external 1574K/14018K, paused 25ms

10-07 19:42:54.122: DEBUG/dalvikvm(20291): GC_EXPLICIT freed <1K, 48% free 2918K/5511K, external 1574K/14018K, paused 25ms

10-07 19:42:54.142: DEBUG/dalvikvm(20291): GC_EXPLICIT freed <1K, 48% free 2918K/5511K, external 1574K/14018K, paused 24ms

10-07 19:42:54.172: DEBUG/dalvikvm(20291): GC_EXPLICIT freed <1K, 48% free 2918K/5511K, external 1574K/14018K, paused 25ms

10-07 19:42:54.202: DEBUG/dalvikvm(20291): GC_EXPLICIT freed <1K, 48% free 2918K/5511K, external 1574K/14018K, paused 25ms

10-07 19:42:54.232: DEBUG/dalvikvm(20291): GC_EXPLICIT freed <1K, 48% free 2918K/5511K, external 14018K/14018K, paused 28ms

10-07 19:42:54.252: DEBUG/dalvikvm(20291): GC_EXPLICIT freed <1K, 48% free 2918K/5511K, external 14018K/14018K, paused 24ms

10-07 19:42:54.282: DEBUG/dalvikvm(20291): GC_EXPLICIT freed <1K, 48% free 2918K/5511K, external 14018K/14018K, paused 24ms

10-07 19:42:54.302: DEBUG/dalvikvm(20291): GC_EXPLICIT freed <1K, 48% free 2918K/5511K, external 14018K/14018K, paused 25ms

10-07 19:42:54.332: DEBUG/dalvikvm(20291): GC_EXPLICIT freed <1K, 48% free 2918K/5511K, external 14018K/14018K, paused 25ms

10-07 19:42:54.362: DEBUG/dalvikvm(20291): GC_EXPLICIT freed <1K, 48% free 2918K/5511K, external 14018K/14018K, paused 25ms

Good day all, i have a load of images i want to display from an sdcard to a gridview. I have followed the tutorial to do this in the is link:

http://mihaifonoage.blogspot.com/2009/11/displaying-images-from-sd-card-in.html

everything works great apart the fact that when the images become a lot, it becomes really slow to populate it and scrolling is also very slow. i have tried editing the code to use the ViewHolder method but still no luck.

now, i also have used populated the images in a custom ListView without using the AsyncTask and it appears to process it faster. yeah common sense would be to just use the faster method but want to clarify somethings first. so i am asking:

  1. is it that AsyncTask can be really slow in some cases and not ideal or thus the fact that am using a Gridview or Listview have something to do with it? any reason why? am asking this cause AsyncTask always seem to get very good favourism.

  2. Any Other way, solutions or tips that i can make this process faster?..

Note: i would have posted my code, but its the same things as the link only that i don't use the getThumbnails() in the MediaStore query. Thanks in advance.

here is part what i get for the logcat output when its loading the images:

10-07 19:42:54.072: DEBUG/dalvikvm(20291): GC_EXPLICIT freed <1K, 48% free 2886K/5511K, external 1574K/14018K, paused 28ms

10-07 19:42:54.092: DEBUG/dalvikvm(20291): GC_EXPLICIT freed <1K, 48% free 2918K/5511K, external 1574K/14018K, paused 25ms

10-07 19:42:54.122: DEBUG/dalvikvm(20291): GC_EXPLICIT freed <1K, 48% free 2918K/5511K, external 1574K/14018K, paused 25ms

10-07 19:42:54.142: DEBUG/dalvikvm(20291): GC_EXPLICIT freed <1K, 48% free 2918K/5511K, external 1574K/14018K, paused 24ms

10-07 19:42:54.172: DEBUG/dalvikvm(20291): GC_EXPLICIT freed <1K, 48% free 2918K/5511K, external 1574K/14018K, paused 25ms

10-07 19:42:54.202: DEBUG/dalvikvm(20291): GC_EXPLICIT freed <1K, 48% free 2918K/5511K, external 1574K/14018K, paused 25ms

10-07 19:42:54.232: DEBUG/dalvikvm(20291): GC_EXPLICIT freed <1K, 48% free 2918K/5511K, external 14018K/14018K, paused 28ms

10-07 19:42:54.252: DEBUG/dalvikvm(20291): GC_EXPLICIT freed <1K, 48% free 2918K/5511K, external 14018K/14018K, paused 24ms

10-07 19:42:54.282: DEBUG/dalvikvm(20291): GC_EXPLICIT freed <1K, 48% free 2918K/5511K, external 14018K/14018K, paused 24ms

10-07 19:42:54.302: DEBUG/dalvikvm(20291): GC_EXPLICIT freed <1K, 48% free 2918K/5511K, external 14018K/14018K, paused 25ms

10-07 19:42:54.332: DEBUG/dalvikvm(20291): GC_EXPLICIT freed <1K, 48% free 2918K/5511K, external 14018K/14018K, paused 25ms

10-07 19:42:54.362: DEBUG/dalvikvm(20291): GC_EXPLICIT freed <1K, 48% free 2918K/5511K, external 14018K/14018K, paused 25ms

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

So尛奶瓶 2024-12-15 20:33:13

它很慢,因为事实上您仍然使用一个单线程(即一个 AsyncTask.execute() 调用)按顺序下载所有图像(即在 doInBackground() 实现中循环所有图像下载)。是的,这就是google建议的使用AsyncTask的方式。

通过调用多个 AsyncTask.execute() 可能会加快速度,这为您提供了多个线程同时运行并由底层线程池管理(在 API 级别 11 之前,此线程池不可访问)。当然,您需要以某种方式将下载任务分成几个部分,并将每个部分输入到每个 AsyncTask.execute() 中:

for (DownloadTask task : tasks) {
  new AsyncTask.execute();
}

查看 此处了解更多信息。

It is slow because in fact you are still using one single thread (i.e. one AsyncTask.execute() call) download all images in sequence (i.e. loop all image download in your doInBackground() implementation). Yes, this is the way that google suggested how to use AsyncTask.

By calling several AsyncTask.execute() will probably speed you up, this gives you several thread running simultaneously and managed by underlying thread pool (before API level 11, this thread pool is non accessible). of cause, you need somehow split your download task into several piece and feed each piece into each AsyncTask.execute():

for (DownloadTask task : tasks) {
  new AsyncTask.execute();
}

Check out here for more information.

烟雨凡馨 2024-12-15 20:33:13

加快速度的正确方法是使用图像缓存。 Google 在此处提供了一个简单实施指南:

The right way to speed this up would be to use a image cache. Google has a guide for an easy implementation right here:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文