0

One day, My customer says "Webview cache seem to stack data area, not cache area. My device is Optimus G 4.4.2 Kitkat".

Whether cache is really accumulate in the data area, i checked my 4.4.2 device.

so, cache was being really accumulated the data area.

and, it does not delete the following way.

protected void trimCache(Context context) {
    try {
        dir = context.getCacheDir();
        if (dir != null && dir.isDirectory()) {
            deleteDir(dir);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

protected boolean deleteDir(File dir) {
    if (dir != null && dir.isDirectory()) {
        children = dir.list();
        for (String aChildren : children) {
            if (deleteDir(new File(dir, aChildren))) continue;
            return false;
        }
    }
    assert dir != null;
    return dir.delete();
}

and, Webview.clearCache() and Context.deleteDatabase("") . 

I have two Question.

  1. How can i delete cache in the data area?
  2. if i can't delete cache in the data area, How i can turn off android webview cache.

p.s. I'm not native speaker, so my english is not enough.

WindSekirun
  • 1,038
  • 11
  • 22

2 Answers2

0

i am using this to clear cache from data.

    try {
            File cache = getCacheDir();
            File appDir = new File(cache.getParent());

            if (appDir.exists()) {
                String[] children = appDir.list();
                for (String s : children) {

                    if (!s.equals("databases") &&!s.equals("lib") && !s.equals("shared_prefs")) {

                        deleteDir(new File(appDir, s));

                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
raj
  • 2,088
  • 14
  • 23
0
How can i delete cache in the data area?

You can take a look at this link for further info as to how to delete the cache data. There is a recursive method that, that guy is using.

if i can't delete cache in the data area, How i can turn off android webview cache.

You can try using

     mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
     mWebView.getSettings().setAppCacheEnabled(false);

But more importantly, just after creating your webview, before loading any pages, you can clear the cache, as mentioned in the documentation here.

mWebView.clearCache(true)

Community
  • 1
  • 1
Antrromet
  • 15,294
  • 10
  • 60
  • 75
  • i tried raj's answer, but seem to not work. so, i decided turn off webview cache when device sdk is 19 or above. – WindSekirun Dec 09 '14 at 11:49