简体   繁体   English

Android:在卸载时从外部存储中删除应用相关文件?

[英]Android: Delete app associated files from external storage on Uninstall?

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. 注意:我需要使用外部存储,因此内部存储或数据库都不合适。

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会自动删除外部存储中的应用程序文件,但文件必须位于应用程序的特定路径中:

"...../Android/data/APP_PACKAGE_NAME/" “..... /安卓/数据/ APP_PACKAGE_NAME /”

where APP_PACKAGE_NAME is the application's package name. 其中APP_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. “..... / Android / obb / APP_PACKAGE_NAME /”,其中APP_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 . obb文件夹用于使用play-store和apk扩展库下载的大文件。 you are not supposed to create files there . 你不应该在那里创建文件。

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. 您的应用也无法收到自己的PACKAGE_REMOVED广播意图,因此您基本上没有通知您正在卸载。

Yes, this is possible. 是的,这是可能的。 Simply write your files to the external files directory: 只需将文件写入外部文件目录:

File dir = getExternalFilesDir(null);

This will create a folder at /Android/data/your.package/. 这将在/Android/data/your.package/创建一个文件夹。 Note that this is not External as in sdcard , but it is publicly accessible. 请注意,这不是sdcard中的 外部 ,但可以公开访问。 If a user uninstalls your app, this directory will also be removed, along with all of its contents. 如果用户卸载了您的应用,则此目录也将被删除,以及其所有内容。

Quoting from the blog post of CommonsWare 引自CommonsWare的博客文章

  • Internal storage: your file is deleted 内部存储:您的文件已删除

  • External storage: if you wrote your file to a location rooted at getExternalFilesDir() or getExternalCacheDir() , your file is deleted. 外部存储:如果您将文件写入以getExternalFilesDir()getExternalCacheDir()根的位置,则会删除您的文件。 If you wrote your file elsewhere (eg, Environment.getExternalStoragePublicDirectory() ), your file is not deleted 如果您在其他地方编写了文件(例如, Environment.getExternalStoragePublicDirectory() ),则不会删除您的文件

  • Removable storage, prior to Android 4.4: removable storage is not officially accessible; Android 4.4之前的可移动存储:可移动存储无法正式访问; 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() or getExternalCacheDirs() ), your file is deleted if that particular bit of removable storage is in the device at the time of uninstall 可移动存储,Android 4.4+:AFAIK,如果您写入受支持的位置( getExternalFilesDirs()getExternalCacheDirs() ),如果卸载时该设备中有特定位的可移动存储,则会删除您的文件

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

相关问题 Android 6.0 - 卸载应用时删除外部存储文件 - Android 6.0 - external storage files being deleted upon app uninstall 在卸载Android 2.3时擦除应用程序外部文件 - Erase app external files on uninstall Android 2.3 卸载时如何删除Android外部存储公共目录中的图片? - How to delete pictures in Android External Storage Public Directory at uninstall? 如果在Android中卸载了应用程序,如何从外部存储中删除文件 - How to delete files from external storage if application gets uninstalled in android Android:从公共外部存储中删除文件 - Android : Delete file from public external storage 如何将图像从 ImageView 保存到 Android 共享存储? 当用户卸载应用程序时,我不希望我的图像被删除 - How to save Image from ImageView to Android Shared Storage? I dont want my images to delete when user uninstall the app 保存在外部缓存中的文件在卸载时不会删除 - files saved in External Cache do not delete on uninstall Android:如何从外部存储中删除文件? - Android: How to remove files from External storage? Android:在应用卸载时从远程数据库中删除记录 - Android: Delete record from remote database on app uninstall 从应用程序上的图库中删除图像文件夹,以编程方式卸载Android - Delete Image Folder from Gallery on App Uninstall Android Programatically
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM