6

I would like to know about how to delete a folder once user select uninstall button for my application. I want it by programmatically is there any chance to do it... If so let me know possible solution for it. thanks in advance.

Shekhar
  • 1,767
  • 3
  • 16
  • 19

2 Answers2

10

If you created any folders on a device's external storage... there is no way for you to call code when the user uninstalls your app. Certain things are removed automatically (databases, anything written to Internal Storage), but not folders on external storage.

EDIT - As pointed out by Stephan, 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.

Maximus
  • 8,351
  • 3
  • 29
  • 37
  • 4
    Correct, just a smal addition: With [Context.getExternalFilesDir()](http://developer.android.com/reference/android/content/Context.html#getExternalFilesDir%28java.lang.String%29) you have directory on the SD card which is automatically removed upon uninstallation. – Stephan Apr 20 '11 at 05:00
  • @Stephan true. Only available on API level 8 or higher though. :) – Maximus Apr 20 '11 at 05:17
  • where I have to add Context.getExternalFilesDir() in my code??? or do I need to change File sdcard = Environment.getExternalStorageDirectory(); to File sdcard = Context.getExternalFilesDir(); I am using API level 8 When I did above thing its getting error. What to do??? – Shekhar Apr 20 '11 at 05:38
  • What error are you receiving? Have you added permission to Write to External Storage to your manifest? – Maximus Apr 20 '11 at 05:46
  • s I have added permissions. Its compile time error: The method getExternalFilesDir() is undefined for the type Context – Shekhar Apr 20 '11 at 06:02
  • I would believe that means that you aren't in fact targeting API Level 8 or higher. – Maximus Apr 20 '11 at 06:07
  • yes I have changed to API Level 8 but its saying cannot make static reference to non-static method my code is: File sdcard = Context.getExternalFilesDir("Recordings"); – Shekhar Apr 20 '11 at 06:12
  • I see. I should have caught that before. getExternalFilesDir() is a non-static method, unlike Environment.getExternalStorageDirectory(). You would call this inside your activity somewhere with either getExternalFilesDir("Recordings"); or YourActivityClass.this.getExternalFilesDir("Recordings"); depending on where you are calling it. – Maximus Apr 20 '11 at 06:16
  • thanks a lot...error is gone...but folder is not getting deleted when I uninstall application – Shekhar Apr 20 '11 at 06:20
  • The application `NQ Mobile Security` is calling an Activity at uninstall look at the http://i.imgur.com/Fos9N.png, http://i.imgur.com/fIZbK.png, http://i.imgur.com/cG9Hr.png and the question http://stackoverflow.com/questions/10219328/how-to-show-an-activity-before-my-app-is-uninstalled-android – Gaurav Agarwal Jun 16 '12 at 10:24
0

Here is an idea. If you are concerned about the fact that the files that are not deleted during an uninstall will be messing up the user onboarding process when he later re-installs your app as in this question then you could simply ensure that all data is deleted at the moment your app is reinstalled (or simply installed for that matter). It's a "if Mohammed does not go to the mountain, the mountain will go to Mohammed" kind of hack. Obviously you would have to set a flag in shared preferences so the process of deleting the content of ExternalStorageDir is only performed once before the users very FIRST interaction with your app here is some sample code:

SharedPreferences sharedPreferences= PreferenceManager.getDefaultSharedPreferences(this);
boolean isFirstInteraction = sharedPreferences.getBoolean("isFirstUsage", true);
if(isFirstInteraction){
    trimCache(this);
    SharedPreferences.Editor editor=sharedPreferences.edit();
    editor.putBoolean("isFirstUsage",false);
    editor.apply();
}
//delete files from external files dir
public static void trimStorage(Context context) {
    try {
        File dir = context.getExternalFilesDir();
        if (dir != null && dir.isDirectory()) {
            deleteDir(dir);

        }
    } catch (Exception e) {
        e.printStackTrace();

    }
}

public static boolean deleteDir(File dir) {
    if (dir != null && dir.isDirectory()) {
        String[] children = dir.list();
        for (int i = 0; i < children.length; i++) {
            boolean success = deleteDir(new File(dir, children[i]));
            if (!success) {
                Log.d("deletion","failed at "+children[i]);
                return false;
            }
        }
    }

    // The directory is now empty so delete it
    return dir.delete();
}
quealegriamasalegre
  • 2,887
  • 1
  • 13
  • 35