Android - 在卸载时保留或删除应用程序创建的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1222269/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android - Preserve or delete files created by the application on uninstall
提问by Niko Gamulin
I created an application which creates and stores files to sdcard. Is there a way to bind the folder with application in order to delete all files when the user runs uninstall on android device?
我创建了一个应用程序,用于创建文件并将其存储到 SD 卡。有没有办法将文件夹与应用程序绑定,以便在用户在 android 设备上运行卸载时删除所有文件?
采纳答案by Prashast
There's no way for your application to know that it is being uninstalled (without modifying the kernel). All files created in the data/data/your.app.package
is deleted automatically upon application uninstall.
您的应用程序无法知道它正在被卸载(不修改内核)。data/data/your.app.package
在应用程序卸载时,将自动删除在 中创建的所有文件。
I don't think anything is cleared from the sdcard. You can do a quick test and find that out.
我认为没有从 SD 卡中清除任何内容。您可以进行快速测试并找出答案。
Another approach could be to have another application that checks whether this application is installed or not. If not, it can do the clean-up work.
另一种方法可能是让另一个应用程序检查是否安装了此应用程序。如果没有,它可以完成清理工作。
回答by rony l
Seems like there have been some developments since 2009 :).
似乎自 2009 年以来有了一些发展:)。
From the documentation:
从文档:
If you're using API Level 8 or greater, use
getExternalCacheDir()
to open a File that represents the external storage directory where you should save cache files. If the user uninstalls your application, these files will be automatically deleted. However, during the life of your application, you should manage these cache files and remove those that aren't needed in order to preserve file space.If you're using API Level 7 or lower, use
getExternalStorageDirectory()
to open a File that represents the root of the external storage, then write your cache data in the following directory:/Android/data//cache/ The is your Java-style package name, such as "com.example.android.app".
如果您使用 API 级别 8 或更高版本,请使用
getExternalCacheDir()
打开一个文件,该文件表示您应该保存缓存文件的外部存储目录。如果用户卸载您的应用程序,这些文件将被自动删除。但是,在应用程序的生命周期中,您应该管理这些缓存文件并删除那些不需要的文件以保留文件空间。如果您使用的是 API 级别 7 或更低级别,请使用
getExternalStorageDirectory()
来打开一个代表外部存储根目录的文件,然后将您的缓存数据写入以下目录:/Android/data//cache/ 是你的Java风格的包名,比如“com.example.android.app”。
回答by Y.S
I'm adding this because the existing answers are outdated in 2017. While it is true that there is no callback on app uninstall, some mechanisms have been provided for preserving app data beyond uninstall.
我添加这个是因为现有的答案在 2017 年已经过时了。虽然没有关于应用程序卸载的回调是真的,但已经提供了一些机制来保留卸载后的应用程序数据。
Since Android 6.0 (API 23), the Auto Backup for Appsfeature was introduced, which allowed developers to either permit or prevent automatic application backup. As of API 23 backup is enabled by default.
Two new
application
tags have been introduced for the manifest file:android:allowBackup
andandroid:fullBackupContent
.
从 Android 6.0 (API 23) 开始, 引入了应用程序自动备份功能,允许开发人员允许或阻止自动应用程序备份。从 API 23 开始,默认启用备份。
application
为清单文件引入了两个新标签:android:allowBackup
和android:fullBackupContent
.
To opt out of automatic backup, add android:allowBackup="false"
to the manifest file under the application
tag. A value of "true" will automatically save shared preferences and databases to the cloud, as well as other files.
要选择退出自动备份,请添加android:allowBackup="false"
到application
标签下的清单文件中。值为“true”将自动将共享首选项和数据库以及其他文件保存到云中。
Secondly, you can be very specific about the files you want to includeor excludefor deletion or preservation on uninstall with the android:fullBackupContent
tag. This attribute points to an XML file that contains backup rules. Create an XML configuration file in the res/xml/
directory with the following syntax:
其次,您可以非常具体地说明要包含或排除的文件,以便在卸载时使用android:fullBackupContent
标记进行删除或保留。此属性指向包含备份规则的 XML 文件。res/xml/
使用以下语法在目录中创建 XML 配置文件:
<full-backup-content>
<include domain=["file" | "database" | "sharedpref" | "external" | "root"]
path="string" />
<exclude domain=["file" | "database" | "sharedpref" | "external" | "root"]
path="string" />
</full-backup-content>
Here you can add the specific file types to include or exclude and their associated paths. Add the android:fullBackupContent = "@xml/my_backup_rules"
tag to the manifest file under the application
tag.
您可以在此处添加要包含或排除的特定文件类型及其关联路径。将android:fullBackupContent = "@xml/my_backup_rules"
标签添加到标签下的清单文件中application
。
The specified files would be backed up to Google Drive under the primary email account of the device. The data would be restored on re-install provided the following setting is enabled by the user:
指定的文件将备份到设备主电子邮件帐户下的 Google Drive。如果用户启用了以下设置,则数据将在重新安装时恢复:
Settings -> Backup & Reset -> Automatic Restore
.
Settings -> Backup & Reset -> Automatic Restore
.
It should also be noted that the backup data cannot be read by the user or other applications on the device.
还需要注意的是,备份数据不能被用户或设备上的其他应用程序读取。
The same can also be accessed from the Google Drive app from here:
也可以从这里从 Google Drive 应用程序访问相同的内容:
Google Drive -> Settings -> Auto backup for apps -> Backup and reset
Google Drive -> Settings -> Auto backup for apps -> Backup and reset
It is worth noting that if the same primaryGoogle account is used for multiple devices, then multiple device backups are created. The backups for individual devices can be accessed from here:
值得注意的是,如果多个设备使用同一个主Google 帐户,则会创建多个设备备份。可以从这里访问单个设备的备份:
Google Drive -> Settings -> Backups
Google Drive -> Settings -> Backups
回答by Michael M
Even when using the getExternalCacheDir()
, I have seen that the cache directory, which is your app's package name, is not removed automatically as it says on the Android Documentation, at least not on the Lenovo A1. It could be a bit naive but you may want to clean up previous data during re-install. There, you can find out if your app was un-installed or is being installed for the first time - you store a value, perhaps a boolean, using shared preferences. It can be something like:
即使在使用 时getExternalCacheDir()
,我也看到缓存目录(即您的应用程序的包名称)不会像 Android 文档中所说的那样自动删除,至少在联想 A1 上不会。这可能有点幼稚,但您可能希望在重新安装期间清理以前的数据。在那里,您可以了解您的应用程序是未安装还是首次安装 - 您可以使用共享首选项存储一个值,可能是布尔值。它可以是这样的:
SharedPreferences sharedPrefs = getSharedPreferences(INSTALL_PREFS, MODE_PRIVATE);
if(sharedPrefs.getBoolean("AppInstalled", false) == false){
//DELETE APP DIRECTORY
SharedPreferences.Editor editor = sharedPrefs.edit();
editor.putBoolean("AppInstalled", true);
editor.commit();
}
This way, everytime the app runs, it checks if it's the first time it's running. When the app is removed, so will the shared preferences for the app. Then, the next time it is installed, the if(...) will be true and you can do some clean up there.
这样,每次应用程序运行时,它都会检查它是否是第一次运行。删除应用程序后,应用程序的共享首选项也将删除。然后,下次安装时, if(...) 将为真,您可以在那里进行一些清理。
I know this doesn't answer the question directly and it's not nice to leave unused data on users' devices, but I think it could be an alternative to trying to clean up when an app is being removed, which is sensible but seems impossible.
我知道这并不能直接回答问题,并且将未使用的数据留在用户的设备上并不好,但我认为这可能是在删除应用程序时尝试清理的替代方法,这是明智的,但似乎不可能。
回答by ákos Maróy
it seems since then there is a proper soltuion, Context.getExternalFilesDir():
从那时起似乎有一个适当的解决方案,Context.getExternalFilesDir():
Returns the absolute path to the directory on the external filesystem (that is somewhere on Environment.getExternalStorageDirectory()) where the application can place persistent files it owns. These files are private to the applications, and not typically visible to the user as media.
This is like getFilesDir() in that these files will be deleted when the application is uninstalled
返回外部文件系统上目录的绝对路径(位于 Environment.getExternalStorageDirectory() 上的某处),应用程序可以在其中放置它拥有的持久文件。这些文件是应用程序私有的,通常作为媒体对用户不可见。
这就像 getFilesDir() 一样,当应用程序被卸载时,这些文件将被删除
回答by Prachi
if you want to store large data then store it in external storage with file path name such as
如果要存储大数据,则将其存储在带有文件路径名的外部存储中,例如
Environment.getExternalStorageDirectory().toString()+"/Android/data/packageName"
where package name is your app package name eg com.think.abc
其中包名称是您的应用包名称,例如 com.think.abc