简体   繁体   中英

Delete cached files - WebView Android 4.4+

I managed to delete cached files created by WebView using:

Clearing android cache , Clear Application cache on exit in android

However for Android 4.4 that solution doesn't work properly, since the files are cached in:

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

instead of:

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

The above path can be obtained by the official command getCacheDir() .

An approach could be hard-coding the path obtained through Get Application Directory

However, is there any [official]/proper solution to address this?

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();
    }
}

Also, here was generated all admob cache 4.4+, you can use a code to verify how many times the user use the app, and delete the admob cache when the user reached the limit.

通常,要清除WebView缓存,请使用此WebView API: WebView.clearCache(true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM