0

In Android 4.0 I use this solution to clear my application cache and it works perfectly:

public void clearApplicationData()
{
    File cache = getCacheDir();
    File appDir = new File(cache.getParent());
    if (appDir.exists()) {
        String[] children = appDir.list();
        for (String s : children) {
            if (!s.equals("lib")) {
                deleteDir(new File(appDir, s));
            }
        }
    }
}

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) {
                return false;
            }
        }
    }
    return dir.delete();
}

Unfortunately, this solution doesn't work in 4.2.2 Android version (and probably in above Android versions too). Anybody knows why? Maybe there is another method to clear cache?

Particulary I am interested in google map cache clearing and solution written above works for me in Android 4.0 but not in Android 4.2.2. Any help would be appreciated.

I don't get any errors in logcat. Device: Samsung Galaxy Tab 2 7.0'

user2999943
  • 2,419
  • 3
  • 22
  • 34
  • No error in the logcat? With which device(s) do you have this behavior? – shkschneider Jan 20 '15 at 14:29
  • 2
    `to clear my application cash` - you want to clear `cash` or `cache`? – Marcin Orlowski Jan 20 '15 at 14:30
  • shkschneider, no error in logcat. Device: Samsung Galaxy Tab 2 7.0' – user2999943 Jan 20 '15 at 14:37
  • Marcin, sorry for mistakes, fixed. I want to clear cache :) – user2999943 Jan 20 '15 at 14:37
  • You can check my answer to this question http://stackoverflow.com/a/14509140/769265 I haven't tested this on recent Android versions, so I can't guarantee it will work. – David Wasser Jan 20 '15 at 14:44
  • The reason this doesn't work is that each application is given a private data area for the cache and applications aren't allowed to access the private data areas of other applications. – David Wasser Jan 20 '15 at 14:45
  • 1
    @David Wasser, I want to clear my OWN application cache from my OWN application code. Unfortunately, your proposed solution didn't worked. – user2999943 Jan 20 '15 at 15:22
  • What have you got in your OWN cache? Post the code you used to put files in your OWN cache. – David Wasser Jan 20 '15 at 15:26
  • 1
    Intentionally I don't store anything in cache. My application contains fragment with google map. Because of it, my application cache size grows to enormous amount when I am using app for a while. After some time I want just clear my application cache. I don't have a problem to do that in Android 4.0 as I wrote above. For some reason I can't clear cache in Android 4.2.2 – user2999943 Jan 20 '15 at 15:34
  • Add some logging so you can tell if you are finding files but failing to clear them, or failing to find them. Also note that files deleted while open will continue to take space until the last file handle is closed. – Chris Stratton Jan 20 '15 at 15:43

1 Answers1

1

I'm writing this as an answer because my comment will probably get buried. Even I had trouble clearing cache in a 4.2.2 device this code by David Wasser in this post worked for me.

PackageManager  pm = getPackageManager();
     // Get all methods on the PackageManager
Method[] methods = pm.getClass().getDeclaredMethods();
for (Method m : methods) {
    if (m.getName().equals("freeStorage")) {
        try {
            long desiredFreeStorage = 8 * 1024 * 1024 * 1024; 
            m.invoke(pm, desiredFreeStorage , null);
        } catch (Exception e) {
           // Method invocation failed. Could be a permission problem
        }
        break;
    }
}
Community
  • 1
  • 1
Abhi
  • 1,512
  • 2
  • 22
  • 46