简体   繁体   English

在缓存图像和数据时,您应该使用内部存储还是外部存储?

[英]When caching images and data should you use Internal or External Storage?

I've read this Android save app settings/data in Internal/External Storage but I don't think i've really got an answer. 在内部/外部存储中读过这个Android保存应用程序设置/数据,但我不认为我真的得到了答案。

I'm developing an app where I should store some "cache" data , like a dozen of images and some strings (json). 我正在开发一个应用程序,我应该存储一些“缓存”数据 ,如十几个图像和一些字符串(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. 最初我将所有这些存储在SD卡上(外部存储)但后来我认为这可能会被SDCard弃用在最近的设备中弃用。

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. 读了一下之后,我明白外部存储不仅仅是SD卡,而是“可移动存储介质(如SD卡)或内部(不可移动)存储”,所以不应该弃用它但是......它是共享的空间,并没有存储在那里的文件的所有权,所以第一个问题是我删除应用程序时无法删除它们。

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? 对于内存较低但SD卡空间较大的设备,这是一种很好的方法吗?

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. 我的应用程序支持从1.6到4.0(到目前为止)所以我有很多遗留设备......我必须让应用程序正常工作(好)。

Looking forward for some interesting answers! 期待一些有趣的答案!

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() . 这让我相信你应该使用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 directories getExternalCacheDir() doesn't provide security so data could be accessed by anyway with access to the SD card. getExternalCacheDir()是在2.2中引入的,所以对你没有任何用处,除非你想检测版本并在2个缓存目录之间切换getExternalCacheDir()不提供安全性所以无论如何都可以通过访问SD来访问数据卡。 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: 评论更新:

although this is a specific case where it's cache...but I don't want it to be deleted whenever the system wants. 虽然这是一个特殊的情况,它的缓存...但我不希望它被系统想要删除。 It's the kind of cache that I need the to app decide when to purge. 这是我需要应用程序决定何时清除的缓存类型。 What is the main concern of storing in on "normal" internal storage without being on the cache dir? 存储在“正常”内部存储而不在缓存目录上的主要问题是什么?

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 评论更新

What do you think about using SharedPreferences to save string data? 您如何看待使用SharedPreferences保存字符串数据? Is there a limit on SharedPreference string saved size? SharedPreference字符串保存大小是否有限制? Is it a (good) possibility? 这是一个(好的)可能性吗?

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. 我已经使用共享偏好来存储过去相对较大的json字符串而没有问题,我发现它比使用数据库的原始数据类型(和字符串)更简单,其中存储的值有限。 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. 此外,您将遇到与存储空间方面的标准内部存储相同的问题。

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 我会去内部

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. 我会将图像保存在外部存储器上,可能是在“隐藏”文件夹中(在文件夹名称的开头添加一个点: .folder ),还有一个Media Scanner“avoider”(. nomedia ),因为,正如你指出的那样,您希望应用程序在旧设备中工作,而且正是这些设备不需要太多内部存储器。

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. 您还可以在应用程序中添加一个选项以删除该文件夹,因此如果用户想要卸载,他可以在此之前执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM