问题描述
我不得不有关是否(以及如何)我应存储从网络加载的图像的问题。比方说,我打电话从我的Android应用程序的Web服务。在这个Web服务,我得到了在网络上的图像的URL。我下载并显示此图像列表项在ListView左侧。我的问题是,我应该使用可能存储图像的方法是什么?我应该:
I had a question related to whether or not (and how) I should store images loaded from the web. Let's say I am calling a web service from my Android app. In this web service I get a URL for an image on the web. I download and show this image on the left side of a list item in a ListView. My question is, what method should I use for possibly storing the image? Should I:
- 将其保存到SD卡,检查它是否在ListView控件创建(在后续请求)的存在,并重新下载必要的(虽然偶尔更新图像,如果它的变化)。
- 将它存储在使用Context.getCacheDir()的缓存,但可能被迫重新下载更多的时候,因为我不能依靠图像留在缓存上。
- 随时下载它,不会存储图像。
图片文件本身是相当小的,但我相信有些用户可能有几十存储下载的这些小图片/。这将工作最好,和/或什么是preferred方法?
The image files themselves are fairly small, but I expect some users could have dozens of these small images downloaded/stored. Which would work best, and/or what is the preferred method?
作为一个方面的问题,我应该在我的ListView加载所有图像的第一个(可能锁定UI一段时间),或装入异步,但显示在此期间,一个占位符图形(这可能是一个更有点丑陋)?有什么标准吗?
As a side question, should I load all of the images in my ListView first (and possibly lock the UI for some time) or load them asynchronously but show a placeholder graphic in the meantime (which might be a bit more "ugly")? What's the standard here?
推荐答案
关于在何处存储:答案取决于什么是被下载了多少。然后你作出选择。
About where to store:The answer depends on what is being downloaded and how much. Then you make a choice.
例如:如果要下载的东西是暂时的,少的数量(少远程取),大小(较少的内存),是当地的一个活动,那么你应该考虑使用SoftReferences拿着照片在内存中。 SoftReferences可导致重新提取,但由于项目的数量是小的,应该实惠。
For instance: If you are downloading something that is temporary, less in number(less remote fetches) and size(less memory) and is local to an Activity then you should consider holding the images in memory using SoftReferences. SoftReferences can lead to refetches but since the number of items is small it should be affordable.
但是,如果要被下载的项数超过某个阈值(这意味着更多的取和内存)就应该考虑通过缓存它们降低取和也运行时内存消耗。在这里,您可以选择将它们保存在SD卡或临时存储(缓存目录的本地应用程序)。对于那些规模小,仅持有这意味着在应用程序(如缩略图)范围内的项目,用户将主要不是你的应用程序之外使用它。所以,你可以在缓存目录中存储这样的事情。使用它们的最好的部分是,你不必打扫残局。它会自动处理。这可能会导致重新抓取虽然。
However, if the number of items to be downloaded exceeds a certain threshold(meaning more fetches and memory) you should consider reducing the fetches and also the Runtime memory consumption by caching them. Here you can either choose to save them on Sdcard or on temporary storage(cache directories local to app). For items that are small and hold meaning only in the context of Application(e.g thumbnails), the user will mostly not use it outside of your application. So, you can store such things in the cache directory. The best part of using them is that you don't have to clean the mess. It is handled automatically. It might lead to re-fetches though.
但是,如果下载的项目规模大,可以在应用程序的环境之外单独存在,如图片,视频,音频剪辑然后SD卡应该是你的选择。你也应该阅读:在BitmapFactory.de codeStream处理大位图,以避免OOM错误(。 。)
However, if the downloaded items are large in size and can stand alone outside the context of the application such as pictures, video, audio clips then SDcard should be your option.You should also read: Handling large Bitmaps to avoid OOM error during BitmapFactory.decodeStream(..)
请注意,您也可以检查,看看是否使用一个数据库,可以帮助这里。见这
Note that you can also check to see if using a database can help here.See this
的几点思考,而在一个ListView延迟加载项:你应该做的在后台加载,而不是阻塞UI thread.You应该考虑出一个临时图像的项目正在下载的同时。这是许多本地应用程序明显。举一个例子实施延迟加载see这。另外,对于大的列表,你可以实现SlowAdapter模式(检查API演示)。它基本上档下载,而列表中滚动。
Some considerations while lazy loading items in a ListView:You should do the loading in the background and not block the UI thread.You should consider showing a temporary image while the item is being downloaded. This is apparent in many native applications. For an example implementation of lazy loading see this.Additionally, for large lists, you can implement the SlowAdapter pattern(check API demos).It basically stalls download while the list is scrolling.
示例项目,可以帮助你在这里:
罗曼盖伊的货架上项目采用二级缓存其中他采用了内存缓存的SD卡(含HashMap的SoftReferences)和存储的
Romain Guy's Shelves project uses two level of caching wherein he employs an in-memory cache(HashMap containing SoftReferences) and storage on Sdcard.Browse Source code here
也有一些开源库,马克·墨菲写(CWAC)和DroidFu,可以帮助这里。
There are also some open source libraries that Mark Murphy wrote(CWAC) and DroidFu that can help here.
祝您好运!
这篇关于Android的 - 从网上下载存储图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!