默认WebView没有开启LocalStorage存储。
代码如下:
mWebView.getSettings().setDomStorageEnabled(true);
mWebView.getSettings().setAppCacheMaxSize(1024*1024*8);
String appCachePath = getApplicationContext().getCacheDir().getAbsolutePath();
mWebView.getSettings().setAppCachePath(appCachePath);
mWebView.getSettings().setAllowFileAccess(true);
mWebView.getSettings().setAppCacheEnabled(true);
或者是:
wvBrowser.getSettings().setJavaScriptEnabled(true);
// 开启DOM缓存。
wvBrowser.getSettings().setDomStorageEnabled(true);
wvBrowser.getSettings().setDatabaseEnabled(true);
wvBrowser.getSettings().setDatabasePath(context.getApplicationContext().getCacheDir().getAbsolutePath());
setDatabasePath在API19时已经废弃,原因是因为在4.4WebView的内核已经换为了Chrome的内核,存储路径有WebView控制。
//加载assets中的html文件
webView.loadUrl("file:///android_asset/index.html");
//加载sdcard中的html文件
webView.loadUrl("file:///mnt/sdcard/index.html");
解决方案:
mWebView.getSettings().setDomStorageEnabled(true);
mWebView.getSettings().setAppCacheMaxSize(1024*1024*8);
String appCachePath = getApplicationContext().getCacheDir().getAbsolutePath();
mWebView.getSettings().setAppCachePath(appCachePath);
mWebView.getSettings().setAllowFileAccess(true);
mWebView.getSettings().setAppCacheEnabled(true);
这个测试了是可以的