Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AppCache correct settings for Android WebView

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?

like image 283
BillyBelly Avatar asked May 07 '15 00:05

BillyBelly


1 Answers

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 :)

like image 81
BillyBelly Avatar answered Nov 19 '22 00:11

BillyBelly