Android:卸载时从外部存储中删除应用程序关联的文件?
如果我正在编写的应用程序将一些文件永久存储到外部存储(因此它们在应用程序退出[销毁]后仍然存在),那会很方便,但是在卸载时我想做一些体面的事情并拥有这些文件删除文件以释放存储空间。
有什么方法可以在卸载时删除这些文件吗?
如果没有(我对此表示怀疑),那么我每次都必须创建这些文件。我试图通过让它们永久存在来节省启动时间并占用所需的空间。
注意:我需要使用外部存储,因此内部存储或数据库都不合适。
It'd be convenient if an application I'm writing stored some files to external storage permanently (so they persist after the application has been exited[destroyed]), but on an uninstall I'd like to do the decent thing and have these files removed to free up the storage.
Is there any way I can have these files removed on an uninstall?
If there isn't (and I'm skeptical), then I'll have to create these files each time. I'm trying to save start-up time and also occupy required space by having them exist permanently.
Note: I need to use external storage, so both internal storage or a DB would be inappropriate.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
其实是有可能的。
android 会自动删除外部存储中应用程序的文件,但这些文件必须位于应用程序的特定路径内:
“...../Android/data/APP_PACKAGE_NAME/”
,其中 APP_PACKAGE_NAME 是应用程序的包名称。
另一个自动清空的路径是:
“...../Android/obb/APP_PACKAGE_NAME/”
其中 APP_PACKAGE_NAME 是应用程序的包名称。
数据适合您想要的任何内容。
obb 文件夹用于使用 play-store 和 下载的大文件apk扩展库。你不应该在那里创建文件。
actually it is possible .
android will automatically remove files of the app in the external storage , but the files must be inside a specific path of the app :
"...../Android/data/APP_PACKAGE_NAME/"
where APP_PACKAGE_NAME is the application's package name.
another path that is automatically being emptied is :
"...../Android/obb/APP_PACKAGE_NAME/"
where APP_PACKAGE_NAME is the application's package name.
the data is for anything you wish.
the obb folder is for huge files that are downloaded using the play-store and the apk extension library . you are not supposed to create files there .
不,我不这么认为。卸载应用程序时(或者用户在应用程序设置应用程序中按下“清除数据”按钮),只有写入内部存储的文件才会被删除。
您的应用也无法接收自己的
PACKAGE_REMOVED
广播意图,因此您基本上不会收到正在卸载的通知。No, I don't believe so. Only files that you write to internal storage will be removed when your application is uninstalled (or if the user presses the 'clear data' button in the Application settings app).
Your app can't receive its own
PACKAGE_REMOVED
broadcast intent either, so you essentially have no notification that you're being uninstalled.是的,这是可能的。只需将文件写入外部文件目录:
这将在 /Android/data/your.package/ 处创建一个文件夹。请注意,这不是 sdcard 中的外部,但它是可公开访问的。如果用户卸载您的应用程序,该目录及其所有内容也将被删除。
Yes, this is possible. Simply write your files to the external files directory:
This will create a folder at /Android/data/your.package/. Note that this is not External as in sdcard, but it is publicly accessible. If a user uninstalls your app, this directory will also be removed, along with all of its contents.
引自CommonsWare的博文
内部存储:您的文件已被删除
外部存储:如果您将文件写入根目录为<的位置code>getExternalFilesDir() 或
getExternalCacheDir()
,您的文件将被删除。如果您将文件写入其他位置(例如,Environment.getExternalStoragePublicDirectory()
),您的文件不会被删除可移动存储,Android 4.4 之前:可移动存储无法正式访问;如果你的文件在那里,那么当你的应用程序被卸载时,它不应该被删除。
可移动存储,Android 4.4+:据我所知,如果您写入受支持的位置(
getExternalFilesDirs()
或< code>getExternalCacheDirs()),如果卸载时该特定的可移动存储位位于设备中,您的文件将被删除Quoting from the blog post of CommonsWare
Internal storage: your file is deleted
External storage: if you wrote your file to a location rooted at
getExternalFilesDir()
orgetExternalCacheDir()
, your file is deleted. If you wrote your file elsewhere (e.g.,Environment.getExternalStoragePublicDirectory()
), your file is not deletedRemovable storage, prior to Android 4.4: removable storage is not officially accessible; if your file winds up out there, it should not be deleted when your app is uninstalled
Removable storage, Android 4.4+: AFAIK, if you write to a supported location (
getExternalFilesDirs()
orgetExternalCacheDirs()
), your file is deleted if that particular bit of removable storage is in the device at the time of uninstall