Android图像缓存-硬HashMap和软HashMaps问题

发布于 2024-11-29 14:17:18 字数 418 浏览 2 评论 0原文

我现在在我的应用程序中尝试做的是修改 ImageDownloader 类,Google 去年在其教程之一中推出的类,它可以异步下载和缓存 ImageView 的图像,而不会泄漏上下文。

换句话说,由于我使用的是全局缓存单例对象,它提供对我的位图 HashMap 的引用,我只需要知道:由于我必须根据应用程序的某些方面(客户端要求)单独缓存图像,我是否应该每种类型的位图都有一对硬和软 HashMap,或者只有一个软 HashMap,其他硬缓存在空间不足时将其文件移动到其中,会更有效吗?

What I'm trying to do right now within my app is modify the ImageDownloader class that Google put out last year in one of their tutorials that asynchronously downloads and caches images for ImageViews without leaking the context.

In other words, since I'm using a global cache singleton object which provides references to my Bitmap HashMaps, I just need to know: since I have to cache images separately depending on certain aspects of my app (client requirement), should I have pairs of hard and soft HashMaps for each of those types of Bitmaps, or would it be more efficient to have only one soft HashMap in which the other hard caches move their files to when they are pressed for space?

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

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

发布评论

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

评论(2

抠脚大汉 2024-12-06 14:17:18

我在我们开发的应用程序上遇到了类似的问题。我们对在 SQLite blob 中拉取的图像进行硬缓存。包装器将检查缓存是否存在,或者通过网络进行拉取,因此即使有人清除了应用程序数据,它也会工作(在图像再次缓存之前速度会慢一些)。由于它位于 SQLite 中,因此应用程序数据可以轻松地在 SD 卡之间移动,而不必担心文件路径被更改。

I've encountered a similar problem on an app we worked on. We hard cache images we pull in an SQLite blob. The wrapper will check the cache's existance, or go pull over the network, so even if someone clears the app data, it'll work (a bit slower until images are cached again). Since it's in SQLite the app data can easily be moved around to the SD card and back without having to worry about file paths being changed.

空气里的味道 2024-12-06 14:17:18

考虑使用类似的东西:

ConcurrentHashMap<String, SoftReference<Bitmap>> image_cache =
newConcurrentHashMap<String, SoftReference<Bitmap>>( 1 );

对于缓存的内存部分。当设备需要内存时,软引用将被垃圾收集。这将使您的应用程序在内存中保留尽可能多的图像,而不会导致内存问题。

您可以选择使用 SD 卡上的文件缓存来支持此缓存,其根路径为:

Environment.getExternalStorageDirectory();

或使用 android 提供的缓存空间,使用以下路径:

context.getCacheDir(); 

缓存空间是私有的,因此可以通过以下方式检索 SD 卡图像 :用户或由第三方程序修改。缓存空间位于设备的内部存储上,并显示在应用程序管理器统计信息中。用户还可以从其设置->应用程序管理器屏幕轻松清除此缓存。

需要填写算法,首先检查内存缓存,如果找到则检查软引用,如果没有找到则检查文件系统,最后从网络获取,保存到文件,放入内存缓存。然后,您可以在此结构之上根据您的客户端要求添加额外的缓存要求。

Consider using something like:

ConcurrentHashMap<String, SoftReference<Bitmap>> image_cache =
newConcurrentHashMap<String, SoftReference<Bitmap>>( 1 );

For the memory portion of your cache. The SoftReference's will be garbage collected as the device needs memory. This will allow your application to keep as many images in memory as possible without causing memory issues.

You can choose to back this cache with a file cache either on the SD card with a root path using:

Environment.getExternalStorageDirectory();

or using the cache space provided by android using a path of:

context.getCacheDir(); 

The cache space is private whereby the SD card images could be retrieved by a user or modified by a 3rd party program. The cache space is on the internal storage of the device and it shows up in the application manager statistics. The user can also clear this cache easily from their settings->application manager screen.

You will need to fill in the algorithm that checks the memory cache first, checks the soft reference if found, checks the file system if not found, and then finally fetches from the network, saves to a file, and puts it in the memory cache. You can then add additional requirements for the cache based on your client requirements on top of this structure.

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