10

I am facing this problem that, when I uninstall my app folder still available in the sdcard path,

What should I do to remove it ?

please suggest something to make it happen ...

Akarsh M
  • 1,629
  • 2
  • 24
  • 47

4 Answers4

15

What should I do to remove it ?

Use getExternalFilesDir() and/or getExternalCacheDir() for your files on external storage ("sdcard"). Those directories are automatically removed when your app is uninstalled.

Beyond this, nothing else is possible, as you do not get control when your app is removed.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
9

Unfortunately, in simple words, the answer is: no, you can't.

Reasons:

  1. You need to broadcast, that you can listen when you app is Uninstalled. but the application which is going to be uninstall won't get any broadcast for its uninstall.

  2. If you created any folders on a device's external storage there is no way for you to call code when the user uninstall your app.

Suggestions and solutions:

  1. Only way to do that is If you use getExternalCacheDir(), then only folders auto deleted when uninstalling the application.

  2. If you are targeting API Level 8 or higher, you can use Context#getExternalFilesDir() for your external files and those will be removed on uninstall.

Community
  • 1
  • 1
Pankaj Arora
  • 10,224
  • 2
  • 37
  • 59
2

Some readings: http://developer.android.com/guide/topics/data/data-storage.html#filesExternal

If you're using API Level 8 or greater, use getExternalFilesDir() to open a File that represents the external storage directory where you should save your files. This method takes a type parameter that specifies the type of subdirectory you want, such as DIRECTORY_MUSIC and DIRECTORY_RINGTONES (pass null to receive the root of your application's file directory). This method will create the appropriate directory if necessary. (...)

If you're using API Level 7 or lower, use getExternalStorageDirectory(), to open a File representing the root of the external storage. You should then write your data in the following directory:

/Android/data/"package_name"/files/

The "package_name" is your Java-style package name, such as "com.example.android.app".

If the user's device is running API Level 8 or greater and they uninstall your application, this directory and all its contents will be deleted.

Waza_Be
  • 39,407
  • 49
  • 186
  • 260
  • getExternalStorageDirectory() gives me the path like : /mnt/sdcards/myfolder/... but when i uninstall my app , folder and data still available ... I'm using the latest API for this app – Akarsh M Apr 13 '13 at 14:11
  • 1
    I wrote A and you complain B doesn't work.... second line: getExternalFilesDir() NOT getExternalStorageDirectory – Waza_Be Apr 13 '13 at 14:33
0

You can't get to run code when your app is uninstalled, so you have to allow the OS to do the clean up for you. This means you can't place the files just anywhere on the SD card, but must follow the rules of the OS.

So rather than create your own directory structure on the SD card, you have to place the files you write in the directories returned by calls to getExternalFilesDir() and getExternalCacheDir(). Android then automatically deletes any contents of these directories when it does the uninstall.

zmarties
  • 4,809
  • 22
  • 39