缓存图像和数据时应该使用内部存储还是外部存储?
我读过这篇Android在Internal/中保存应用程序设置/数据外部存储,但我认为我还没有真正得到答案。
我正在开发一个应用程序,我应该在其中存储一些“缓存”数据,例如十几个图像和一些字符串(json)。
最初我将所有内容存储在 SD 卡(外部存储)上,但后来我认为这可能会因大多数最新设备中的 SDCard 弃用而被弃用。
读了一下之后,我了解到外部存储不仅仅是 SD 卡,而是“可移动存储介质(例如 SD 卡)或内部(不可移动)存储”,因此不应弃用它,但是......它是共享的空间,并且存储在那里的文件没有所有权,所以第一个问题是当应用程序被删除时我无法删除它们。
所以我更改为内部存储强>,以避免文件/图像“公开”,并在应用程序删除后将其删除。
这是推荐的方法吗?
在内部存储空间较低但 SD 卡中有大量空间的旧设备上,这是一个好方法吗?
我的应用程序支持从 1.6 到 4.0(到目前为止),所以我有很多旧设备...并且我必须让该应用程序在所有设备上都能(良好)运行。
期待一些有趣的答案!
I've read this Android save app settings/data in Internal/External Storage but I don't think i've really got an answer.
I'm developing an app where I should store some "cache" data, like a dozen of images and some strings (json).
Initially I was storing all that on the sdcard, (external storage) but later i thought that this could be deprecated by the SDCard deprecation in most recent devices.
After reading a bit, I understood that external storage is not only sdcard, but "a removable storage media (such as an SD card) or an internal (non-removable) storage" so it should not be deprecated but...it's shared space, and there is not ownership over the files stored there, so the first problem was that I was unable to delete them when the app was deleted.
So I changed to the Internal Storage, to avoid having the files/images "public" and also having them removed after app deletion.
Is this the recommended approach?
On devices older with low internal storage but with a lot of space in the SDcard is this a good approach?
My application supports from 1.6 to 4.0 (so far) so I have a lot of legacy devices... and I must have the app working (well) on all.
Looking forward for some interesting answers!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这取决于您想要存储的数据类型。
你提到它是缓存的数据,所以我的假设是,如果由于某种原因它全部消失了也没关系。这让我相信您应该使用
getCacheDir()
。在这种情况下,如果缓存变得太大,系统将删除文件,因此内部存储空间较低的设备不会出现问题(尽管建议您自行管理),它相对安全,并且将由应用程序管理因此,如果有卸载,它将被删除。getExternalCacheDir()
是在 2.2 中引入的,因此对您没有任何用处,除非您想检测版本并在 2 个缓存目录之间切换getExternalCacheDir()
不会提供安全性,以便可以通过任何方式访问 SD 卡来访问数据。我认为您可能想要这样做的唯一原因是您想要的缓存大小,但从您的描述来看,数据似乎并不过分。根据评论更新:
如果您进入系统因存储空间太低而清理内部缓存数据的阶段,那么您可能应该让它来清理此类应用程序数据。通过使用标准内部数据存储,您将绕过此安全防护,这可能会产生比删除应用程序数据更令人不快的问题。
如果您的数据非常重要,那么我建议尝试识别更重要的特定数据并单独管理。如果您确定的这些数据需要安全,那么使用文件或数据库(取决于数据类型)的内部存储似乎是您唯一真正的选择,但您必须警惕这些数据的积累。
根据评论更新
我过去曾使用共享首选项来存储相对较大的 json 字符串,没有任何问题,我发现它比使用原始数据类型(和字符串)的数据库更简单,因为要保存的值有限。然而,当您拥有图像或大量值时,管理就会变得更加复杂。此外,在存储空间方面,您还会遇到与标准内部存储相同的问题。
It depends on the type of data you are wanting to store.
You mention it's cached data so myassumption is that it should not matter if for some reason it all disappears. This leads me to believe that you should be using the
getCacheDir()
. In this case the system will remove files if the cache becomes too big so devices with low internal storage shouldn't present a problem (although it is recommended to manage this your self anyway), it's relatively secure and it will be managed by the app so if there is an uninstall it will be removed.getExternalCacheDir()
was introduced in 2.2 so isn't any use to you unless you would like to detect the version and switch between the 2 caching directoriesgetExternalCacheDir()
doesn't provide security so data could be accessed by anyway with access to the SD card. The only reason I could think you might want to do this is because of size of cache you desire but from your description the data doesn't seem excessive.UPDATED from comment:
If you get to the stage where the system is cleaning up internal cached data because storage is so low then you should probably leave it to clean up this sort of app data. By using standard internal data storage you are bypassing this safe guard which would probably create more unpleasant problems than having app data deleted.
If your data is of high importance then I would suggest trying to identify specific data that is more important and managing that separately. If this data you identify needs to be secure then internal storage using files or a db (depending on the data type) seems like your only real option but you would have to be wary of this data building up.
UPDATED from comment
I have used shared preferences to store relatively big json strings in the past with no problem, I find it's simpler than using the databases for primitive data types (and strings) where there are limited values to save. However when you have images or lot's of values, management becomes more complex. Also you will have the same problem as you would with standard internal storage in terms of storage space.
我会将图像保留在外部存储上,可能在“隐藏”文件夹中(在文件夹名称的开头添加一个点:.folder)以及媒体扫描仪“避免器”( >.nomedia),因为正如您所指出的,您希望应用程序在旧设备上运行,而恰恰这些设备不需要太多内存。
您还可以在应用程序中添加一个选项来删除该文件夹,因此如果用户想要卸载,他可以在之前执行此操作。
I would keep images on the external storage, probably in a "hidden" folder (adding a dot at the beggining of the folder's name: .folder) and also a the Media Scanner "avoider" (.nomedia), because, as you pointed, you want the application to work in old devices, and precisely those devices don't have to much internal memory.
You can also add an option in your application to remove that folder, so in case the user wants to uninstall, he can do that before.
我会使用内部存储作为缓存。这将消除用户访问您的应用程序中使用的文件的机会。我会选择内部
I would use internal storage for cache. This will take away the chance of a user being able to access the files used in your app. I would go for internal