10

I'm trying to figure out which are the correct settings to enable appcache on android webview. I found many discussions about that but none of them worked.

Given that AppCache is set correctly (it works on chrome), my wrong settings on the webview are the following:

mWebView = (WebView) findViewById(R.id.activity_main_webview);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDatabaseEnabled(true);
webSettings.setDomStorageEnabled(true);
webSettings.setAllowFileAccess(true);
webSettings.setAppCachePath("/data/data/"+ getPackageName() +"/cache");
webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
webSettings.setAppCacheEnabled(true);
webSettings.setSupportMultipleWindows(true);
mWebView.setVerticalScrollBarEnabled(false);
mWebView.loadUrl("http://www.myapp.com");

Any idea of why it doesn't work?

BillyBelly
  • 523
  • 3
  • 14

1 Answers1

21

FOUND THE SOLUTION:

The app cache path wasn't set correctly. I'm now using the follownig code to define the path:

String appCachePath = activity.getCacheDir().getAbsolutePath();
webSettings.setAppCachePath(appCachePath);

Instead of the old version:

webSettings.setAppCachePath("/data/data/"+ getPackageName() +"/cache");

Hope will be useful for other developers :)

Mohammad Ersan
  • 12,304
  • 8
  • 54
  • 77
BillyBelly
  • 523
  • 3
  • 14
  • It was useful for me, thank you so much, it came in handy – Kyle Nov 24 '15 at 11:42
  • webSettings.setAppCachePath("/data/data/"+ getPackageName() +"/cache"); is deprecated in version 33. solution please ? – Imran khan Feb 27 '23 at 13:53
  • @Imrankhan what is the solution for `webSettings.setAppCachePath("/data/data/"+ getPackageName() +"/cache");` because I am getting error here, please do some help? – Ninja Aug 14 '23 at 07:25