Android 中的线程安全渐进式下载
我正在编写一个Android应用程序,它必须下载相当多的图像文件;以前的开发人员使用各种 HttpClient 类型类来编写它。这些不提供任何进度信息,如果下载中途失败,则无法恢复丢失的位(据我所知)。
我们希望能够向用户报告进度,特别是如果速度很慢,并且我们希望能够恢复下载,因此我使用 HttpUrlConnection 重新实现了这一点。它在单线程下运行良好,但在多线程下运行不佳:我不知道到底发生了什么,但图像正在损坏。 Android 表示这种情况确实不是线程安全的。
据我所知,这意味着我们必须在多线程下载(我的同事报告稍快)和恢复能力之间进行选择。除了实现我自己的线程安全 HTTP 连接类之外,有什么方法可以在 Android 上同时实现这两种功能吗?
I am writing an Android application that has to download quite a lot of image files; the previous dev wrote it using various HttpClient type classes. These give no progress information whatever, and if the download fails part-way through there is no way to recover the missing bits (as far as I can find out).
We want to be able to report progress to the user, particularly if it is slow, and we want to be able to resume downloads, so I reimplemented this using HttpUrlConnection. It works fine single-threaded, but not multithreaded: I don't know what happens exactly but the images are getting corrupted. Android says that instances of this are indeed not thread safe.
As far as I can tell this means we have to choose between multithreaded downloads (which my colleague reports are slightly faster) and the ability to resume. Is there any way to get both on Android, short of implementing my own, thread-safe HTTP connection class?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想到两件事。 AsyncTask 类(请参阅:Painless Threading)是为这种类型构建的事情,并且 新的 DownloadManager 类被开发为一个相当强大的方法,重新启动如有必要,在设备重新启动等过程中仍然存在。显然,这实际上取决于您的情况,但请看一下它们是否是您需要的。
Two things come to mind. The AsyncTask class (see: Painless Threading) is built for this sort of thing, and the new DownloadManager class was developed as a rather robust method that restarts if necessary, persists through device restarts, etc. Obviously it really depends on your situation, but give those a look and see if they're something you need.