Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 922
Rod Avatar asked Nov 10 '22 15:11

Rod


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

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.

like image 180
Santiago Avatar answered Nov 14 '22 23:11

Santiago