Delete cached files - WebView Android 4.4+

I was able to delete cached files created by WebView using:

Clear Android Cache , Clear Application Cache on Android

However, for Android 4.4, this solution does not work correctly, as files are cached in:

/data/data/com.app.package/app_webview/

instead:

/data/data/com.app.package/cache/

The above path can be obtained using the official team getCacheDir().

The approach may be to hardcode the path obtained through Get Application Catalog

However, is there an [official] / correct solution to solve this problem?

+4
source share
2 answers

you can use this code

    private static boolean deleteDir(File dir) {
    if (dir != null && dir.isDirectory()) {
        String[] children = dir.list();
        for (String aChildren : children) {
            boolean success = deleteDir(new File(dir, aChildren));
            if (!success) {
                return false;
            }
        }
    }
    // The directory is now empty so delete it
    return dir != null && dir.delete();

}

void trimCache() {

    try {
        String pathadmob = this.getFilesDir().getParent() + "/app_webview";
        File dir = new File(pathadmob);
        if (dir.isDirectory()) {
            deleteDir(dir);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

, admob cache 4.4+, , , admob, .

+1

, WebView, API WebView: WebView.clearCache(true);

0

All Articles