9

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.

bdls
  • 4,578
  • 6
  • 24
  • 30

4 Answers4

10

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 .

android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • I've come across and application with persistent data that stays after the apk is uninstalled and it's data from ../Android/data/APP is deleted. Also, anywhere with the APP name has been deleted, but there is still some remnant files created by the APP that is still in the system. I'm just wondering where could it possibly be hiding? – naisanza Jan 31 '13 at 00:18
  • i don't understand. can you please explain some more? – android developer Jan 31 '13 at 06:53
  • @androiddeveloper How do you apply this `/Android/data/APP_PACKAGE_NAME/`? In my current app, I'm using `Environment.getExternalStorageDirectory().getPath() + "/SAMPLE/"` it's not deleted during uninstall. – Compaq LE2202x Feb 06 '14 at 03:29
  • @CompaqLE2202x do you by any chance uninstall the app on an old device/OS ? i remember i saw some devices that had this problem. – android developer Feb 06 '14 at 06:34
  • @androiddeveloper I'm afraid not, my app target devices run on 4.0 and higher. I'm pretty sure using `getExternalStorageDirectory()` would not delete its created files, only those with `/Android/data/APP_PACKAGE_NAME/`, I just don't know how to apply it.. – Compaq LE2202x Feb 06 '14 at 06:44
  • @CompaqLE2202x again, what is your device that you are testing, and which android OS does it have? also, I assume that you've replaced "APP_PACKAGE_NAME" with the real package name of your app. – android developer Feb 06 '14 at 07:28
  • @androiddeveloper I have no specific device, my app's target devices run on Android 4.0 and higher. Furthermore, I just discovered in order to apply `/Android/data/APP_PACKAGE_NAME/` in code, is `getExternalFilesDir()` which is equivalent to `Android/data/data/your_package/`. Took it from [here](http://stackoverflow.com/a/10124040/1968739). – Compaq LE2202x Feb 06 '14 at 07:45
  • @CompaqLE2202x then how do you say that when the app gets uninstalled this folder won't be removed, if you didn't test it out yourself on a real device? and what does it mean "my app's target devices run on Android 4.0 " ? what did you put in the manifest, and why do you think it matters in this case (we are talking about uninstallation, right?) ? – android developer Feb 06 '14 at 07:59
  • @androiddeveloper I tested it in my test device Sony Xperia SOL22 but what I meant in `do you by any chance uninstall the app on an old device/OS ?` is that there's no specific device, as long it's running in Android version 4.0 or ICS or API level 14 or higher(answering from `on an old device/OS ?` even though it's unrelevant).. – Compaq LE2202x Feb 06 '14 at 08:11
  • @CompaqLE2202x what is the android OS version on the device you've tested the uninstallation on? according to some websites i see it's 4.1, which is weird since it's quite new. about the target, i still don't get what you mean. what do you have in the manifest? – android developer Feb 06 '14 at 08:21
  • @androiddeveloper OS version is 4.1.2. Target device means the devices that my client should have using my app, and it should start at OS version 4.0 or Ice Cream Sandwich or higher. So, old devices' problems are not really my concern. It's just that having an external folder for my app files does not include in deletion unless I save it internally. My targetSdkVersion in Manifest is 17 and minSdkVersion is 11. – Compaq LE2202x Feb 06 '14 at 09:18
  • @CompaqLE2202x targetSdkVersion should always be set to the maximum available (19 is currently the latest). also, as of API 19, if your app only writes to its official path on the external storage, you don't need write-permission. anyway, this can't be the reason for your problem. please try to install other apps that store data to the external storage , and maybe even large games (that exceed the 50MB restriction) , to see if this happens for them too. if the folders don't get deleted , it's a bug of the manufacturer, since those folders should be deleted upon uninstallation . – android developer Feb 06 '14 at 09:22
7

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.

Christopher Orr
  • 110,418
  • 27
  • 198
  • 193
  • Just to clarify, you can't create a broadcast receiver as part of your app that listens for this? – Ally Jan 01 '10 at 23:27
  • 1
    You can create a broadcast receiver that listens for any `PACKAGE_*` events the system sends, but you won't receive them for your own application *except* for when your application is being upgraded -- you'll get `PACKAGE_REMOVED` followed soon after by `PACKAGE_REPLACED`. – Christopher Orr Jan 02 '10 at 01:42
4

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.

Phil
  • 35,852
  • 23
  • 123
  • 164
2

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

Devrath
  • 42,072
  • 54
  • 195
  • 297