在Android的缓存文件时的最佳实践 [英] Best Practice when Caching files in Android

查看:151
本文介绍了在Android的缓存文件时的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我已经在缓存子目录的应用我的应用程序缓存的图像文件。这些图像是用在的ListView 并存储在的HashMap SoftReferences 位图

I currently have my app caching image files in the cache sub-directory for the application. The images are used in a ListView and stored in a HashMap of SoftReferences to Bitmaps.

所以,我的问题是,什么是缓存这些图像文件,无需夸大我的应用程序使用,仍然从用户的角度来看响应空间的最佳方式。

So my question is this, what is the best way to cache these image files without inflating the space my application uses AND remains responsive from a user standpoint.

东西我关心的:

我知道用户可以清除缓存,当空间不足的内存是自动完成的,但我觉得大多数用户将看到一个几MB的应用程序,并卸载它。此外,如果空间不断低,我的应用程序将只保留下载图像,使其显得更慢。

I know the user can clear the cache and that it is done automatically when space is low on internal memory, but I feel most users will see a several MB app and uninstall it. Also if the space is constantly low, my app will just keep downloading the images, making it appear slower.

大多数设备有一个SD卡pre-安装,但我应该怎么办不插的时候吗? SD卡也可能比较慢到内部存储器,影响了我的应用程序的性能。

Most devices have an SD card pre-installed, but what should I do when it is not inserted? The SD card may also be slower compared to internal storage, affecting my app's performance.

我应该包括一个选项,选择高速缓存的位置?

Should I include an option to choose the location of the cache?

我应该尝试管理我缓存的大小(无论是在/缓存或/ SD卡)或只是忘了呢?

Should I attempt to manage the size of my cache (be it in the /cache or /sdcard) or just forget about it?

感谢您的时间(它的一个漫长的,我知道),并请张贴任何相关经验。

Thank you for your time (its a long one I know) and please post any relevant experience.

推荐答案

我不能给你一个COM prehensive一套最佳做法,但我可以提供我所学会为止:

I can't offer you a comprehensive set of best practices, but I can offer what I've learned so far:

管理您的缓存是一个不错的主意。我的应用程序的缓存是这样的,我知道,我永远不会需要超过一定数量的缓存文件,所以每当我插入一个新的文件到缓存中,我删除旧的文件,直到我在我所设置的限制。你可以做类似的东西的基础上的大小,或者干脆年龄。

Managing your cache is a good idea. My app's cache is such that I know that I'll never need more than a certain number of cached files, so whenever I insert a new file into the cache, I delete the oldest files until I'm under the limit I have set. You could do something similar based on size, or simply age.

缓存到SD卡上,如果它是可用的,如果是你的缓存需要占用大量的空间是一个好主意。你需要同样仔细管理的空间,因为它不会自动清除为你的空间。如果你正在缓存的图像文件时,请务必把他们与一个点开始,像/yourAppHere/.cache的目录。这将保持图像的显示在画廊,这实在是烦人了。

Caching to the SD card, if it's available, is a good idea if your cache needs to take up a lot of space. You'll need to manage that space just as carefully, since it won't automatically clear that space for you. If you're caching image files, be sure to put them in a directory that begins with a dot, like "/yourAppHere/.cache". This will keep the images from showing up in the gallery, which is really annoying.

让用户选择缓存的位置似乎有点小题大做了我,但如果你的观众是非常令人讨厌,它可能是AP preciated。

Letting the user choose the location of the cache seems like overkill to me, but if your audience is very geeky, it might be appreciated.

我缓存到SD的时候有没有注意到一个太大的惩罚,但我不知道你的应用程序使用这些信息。

I haven't noticed a much of a penalty when caching to the SD, but I don't know how your app uses that information.

这篇关于在Android的缓存文件时的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆