Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 936
bdls Avatar asked Jan 01 '10 22:01

bdls


People also ask

How do I get rid of leftover files after uninstalling Android apps?

Select “Clear data” and/or “Clear cache.” Depending on the app, there may also be a “Manage data” option to clear additional settings and data. For instance, a browser app may have this option to delete bookmarks and stored passwords.

Does uninstalling app delete data?

App data and cache is deleted. But any folders/files the app makes in your storage directory will not be removed.


2 Answers

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

like image 194
Phil Avatar answered Oct 10 '22 20:10

Phil


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() or getExternalCacheDir(), your file is deleted. If you wrote your file elsewhere (e.g., Environment.getExternalStoragePublicDirectory()), your file is not deleted

  • Removable 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() or getExternalCacheDirs()), your file is deleted if that particular bit of removable storage is in the device at the time of uninstall

like image 35
Devrath Avatar answered Oct 10 '22 20:10

Devrath