使用内部存储 - Android - 删除缓存文件
我正在下载图像,然后将它们保存到内存中。这导致应用程序的缓存增长非常快。
正如此处建议的那样:
http://developer.android.com/guide /topics/data/data-storage.html
当设备内部存储空间不足时,Android 可能会删除这些缓存文件以恢复空间。但是,您不应该依赖系统来为您清理这些文件。您应该始终自己维护缓存文件并保持在合理的空间消耗限制内,例如 1MB
我的问题是如果缓存太大会发生什么,例如现在约为 20mb 并且仍在增长。
我认为如果互联网连接可用,我应该遵循指南并在每次应用程序启动时删除它们。
String[] allFiles = fileList();
Log.e(TAG, "allFiles.length: " + allFiles.length);
for(int k=0; k<allFiles.length; k++)
{
Log.e(TAG, "allFiles [ " +k+"]: "+allFiles[k]);
}
但是,此代码始终返回零文件。如果我尝试访问特定图像,我可以轻松地从内存中检索它。为什么此代码无法返回包含我所有图像的数组?
内部存储器遵循的正确方法是什么..
提前致谢,
安德烈亚斯
I am downloading images and then saving them to the internal memory. This has a result the cache of the app to grow very quickly.
As it is suggested here:
http://developer.android.com/guide/topics/data/data-storage.html
When the device is low on internal storage space, Android may delete these cache files to recover space. However, you should not rely on the system to clean up these files for you. You should always maintain the cache files yourself and stay within a reasonable limit of space consumed, such as 1MB
My question is what will happen if the cache gets too big, for example now is around 20mb and is still growing.
I thought that I should follow the guidelines and delete them each time my app starts if internet connection is available.
String[] allFiles = fileList();
Log.e(TAG, "allFiles.length: " + allFiles.length);
for(int k=0; k<allFiles.length; k++)
{
Log.e(TAG, "allFiles [ " +k+"]: "+allFiles[k]);
}
However, this code always return zero files. If i try to access a specific image I can easily retrieve it from the internal memory. Why this code cannot return an array with all my images??
What is the right approach to follow for the internal memory..
Thanks in advance,
Andreas
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
昨天还有人问了类似的问题。 这是我对此问题的回答。不过,它的一些相关关键点是您可以限制整个缓存大小和/或限制将文件保留在缓存中的时间,这应该可以解决您的问题。
Somebody else asked a similar question just yesterday. Here's my answer to that one. Some relevant key points of it though is that you can limit the whole cache size and/or limit the amount of time you keep a file in your cache and that should solve your problem.