Android - 保留或删除应用程序卸载时创建的文件

发布于 2024-07-29 22:27:26 字数 85 浏览 4 评论 0原文

我创建了一个应用程序,用于创建文件并将其存储到 SD 卡。 有没有办法将文件夹与应用程序绑定,以便当用户在 Android 设备上运行卸载时删除所有文件?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

诗酒趁年少 2024-08-05 22:27:26

自 2009 年以来似乎有了一些进展:)。

来自文档

如果您使用的是 API 级别 8 或
更大,使用 getExternalCacheDir()
打开一个代表的文件
您所在的外部存储目录
应该保存缓存文件。 如果用户
卸载您的应用程序,这些
文件将被自动删除。
然而,在你的一生中
应用程序,你应该管理这些
缓存文件并删除那些
不需要为了保存
文件空间。

如果您使用的是 API 级别 7 或更低版本,
使用 getExternalStorageDirectory() 来
打开代表根的文件
的外部存储,然后写入
您的缓存数据如下
目录:

/Android/数据//缓存/
这是你的Java风格
包名,例如
“com.example.android.app”。

Seems like there have been some developments since 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".

倾听心声的旋律 2024-08-05 22:27:26

我添加此内容是因为现有答案在 2017 年已过时。
虽然应用程序卸载确实没有回调,但已经提供了一些机制来保留卸载后的应用程序数据。

  • 自 Android 6.0 (API 23) 起,自动备份
    应用

    引入了该功能,该功能允许开发人员允许或阻止自动应用程序备份。 自 API 23 起默认启用备份。

  • 清单文件引入了两个新的 application 标签:android:allowBackupandroid:fullBackupContent

要选择退出自动备份,请将 android:allowBackup="false" 添加到 application 标记下的清单文件中。 值为“true”将自动将共享首选项和数据库保存到云端,以及 其他文件

其次,您可以非常具体地指定要在卸载时包含排除删除或保留的文件使用 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>

您可以在此处添加要包含或排除的特定文件类型及其关联路径。 将 android:fullBackupContent = "@xml/my_backup_rules" 标记添加到 application 标记下的清单文件中。

指定的文件将备份到设备主电子邮件帐户下的 Google 云端硬盘。 只要用户启用以下设置,重新安装时数据就会恢复:

设置 -> 备份与备份 重置-> 自动恢复。

还应该注意的是,用户或设备上的其他应用程序无法读取备份数据。

输入图像描述这里

也可以从此处的 Google 云端硬盘应用程序访问该内容:

Google 云端硬盘 -> 设置-> 应用程序自动备份 -> 备份和重置

在此处输入图像描述

值得注意的是,如果多个设备使用同一个 Google 帐户,则会创建多个设备备份。 可以从此处访问各个设备的备份:

Google Drive -> 设置-> 备份

Google 云端硬盘 -> 设置 -> 备份

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.

  • Since Android 6.0 (API 23), the Auto Backup for
    Apps

    feature 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 and 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.

Secondly, you can be very specific about the files you want to include or exclude for 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:

<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.

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:

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.

enter image description here

The same can also be accessed from the Google Drive app from here:

Google Drive -> Settings -> Auto backup for apps -> Backup and reset

enter image description here

It is worth noting that if the same primary Google account is used for multiple devices, then multiple device backups are created. The backups for individual devices can be accessed from here:

Google Drive -> Settings -> Backups

Google Drive -> Settings -> Backups

疑心病 2024-08-05 22:27:26

您的应用程序无法知道它正在被卸载(不修改内核)。 应用程序卸载后,data/data/your.app.package 中创建的所有文件都会自动删除。

我认为 SD 卡上没有清除任何内容。 您可以进行快速测试并找出答案。

另一种方法可能是使用另一个应用程序来检查该应用程序是否已安装。 如果没有,它可以完成清理工作。

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.

I don't think anything is cleared from the sdcard. You can do a quick test and find that out.

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.

翻身的咸鱼 2024-08-05 22:27:26

即使在使用 getExternalCacheDir() 时,我也发现缓存目录(即应用程序的包名称)不会像 Android 文档中所述自动删除,至少在 Lenovo 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();
}

这样,每次应用程序运行时,它都会检查是否是第一次运行。 当应用程序被删除时,应用程序的共享首选项也将被删除。 然后,下次安装时,if(...) 将为 true,您可以在那里进行一些清理。

我知道这并不能直接回答问题,而且在用户设备上保留未使用的数据并不好,但我认为这可能是在删除应用程序时尝试清理的另一种选择,这是明智的,但似乎不可能。

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:

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.

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.

于我来说 2024-08-05 22:27:26

似乎从那时起就有了一个合适的解决方案, Context.getExternalFilesDir()

返回外部文件系统上目录的绝对路径
(位于 Environment.getExternalStorageDirectory() 上的某个位置)
应用程序可以放置它拥有的持久文件。 这些文件是
对于应用程序来说是私有的,并且通常对用户不可见
媒体。

这就像 getFilesDir() 一样,这些文件将在以下情况下被删除:
应用程序已卸载

it seems since then there is a proper soltuion, 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

玩物 2024-08-05 22:27:26

如果您想存储大数据,请将其存储在外部存储中,并使用文件路径名称,例如

Environment.getExternalStorageDirectory().toString()+"/Android/data/packageName"

其中包名称是您的应用程序包名称,例如 com.think.abc

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文