是否在应用程序更新时删除了内部存储文件? [英] Are Internal Storage Files Deleted on App Update?

查看:140
本文介绍了是否在应用程序更新时删除了内部存储文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

众所周知:

当用户卸载您的应用程序时,系统会删除您所有应用程序的 内部存储中的文件.

When the user uninstalls your app, the system removes all your app's files from internal storage.

用户更新应用程序时会发生同样的事情吗?

Does the same thing happens when the user update the app?


我为什么要问?,我在raw/文件夹中有一个文件,每次运行我的应用程序(如果尚未移动)时,该文件都会移至内部存储器:


Why Am I asking? I have a file in the raw/ folder which I move to the Internal Storage every time my app is run (if it is not already been moved):

//On Application onCreate() method:
InputStream jsonFile = ctx.getResources().openRawResource(R.raw.data);
File file = ctx.getFileStreamPath(Helper.FILE_NAME);
if(file.exists())
    return; //Exit because the file is already been copied.
FileOutputStream fos = ctx.openFileOutput(Helper.FILE_NAME, Context.MODE_PRIVATE);
copyFile(fos, jsonFile); //my method to copy files.

该文件可能会在该应用程序的未来版本中更新.如果系统在更新应用程序时删除了内部文件,则上面的代码将确定.但是,如果不是这种情况,那么我需要实施另一项检查 以确保用户具有最新版本 .像这样:

The file might be updated in future releases of the app. if the System deletes internal files upon updating the app, the above code will be Ok. However, if this is not the case, then I need to implement another check to ensure the user has the latest version. Something like this:

if(file.exists() && fileVersion == lastVersion) 

推荐答案

仅当您卸载并重新安装时,才会删除内部文件.升级将删除内部文件-不应这样做.考虑到这样做,您的数据库也将在每次升级时被删除.

Internal files are removed only if you uninstall and reinstall. An upgrade will not remove internal files -- and it shouldn't. Consider that if it did that, your database would be removed for every upgrade as well.

您将需要添加其他支票.

You will need to add an additional check.

作为参考,以下文档以一种about回的方式对此进行了说明:

For reference, the following documentation explains this in a roundabout way:

存储选项

请注意第一段:

Android提供了多个选项来保存永久 应用程序数据.

Android provides several options for you to save persistent application data.

(添加了重点)

进一步在内部存储下,它说

当用户卸载您的应用程序时,这些文件将被删除.

When the user uninstalls your application, these files are removed.

我省略了一个结论,因为这些选项是永久性的",并且仅明确指出在卸载时文件将被删除,因此升级将保留这些文件.

By omission I conclude that since these options are "persistent" and it's only explicitly stated that the files will be removed on uninstall, that an upgrade will retain those files.

我从未见过在升级过程中删除内部文件的情况,因此从经验上讲,这也适用.

I've never seen an internal file removed during upgrade, so experientially, this holds true as well.

对于它的价值,如果raw中的文件是只读的,则甚至不需要复制它. openRawResource()将为您提供InputStream,并允许您像访问其他任何文件一样对其进行访问.在内部,就是这样:一个文件.

For what it's worth, if the file you have in raw is read-only, you don't even need to copy it. openRawResource() will give you an InputStream and allow you to access it as any other file. Internally, it's just that: A file.

复制到外部存储不是一个很好的选择,因为用户(在早期的Android版本中,任何其他应用程序)都可以访问,删除或修改该文件.

Copying to external storage isn't a good alternative as the user (and in earlier Android versions, any other app) can access, delete or modify that file.

这篇关于是否在应用程序更新时删除了内部存储文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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