【发布时间】:2017-09-14 06:08:36
【问题描述】:
我正在 android studio 上开发 webview 应用程序。
问题是我的应用没有缓存超过 20.0MB。
我从 Settings>ApplicationManager>MyWebviewApp>Cache 检查了它。
当缓存达到超过 20.0MB 时,旧缓存似乎已被删除。
我可以增加缓存容量吗?
【问题讨论】:
标签: android webview browser-cache
我正在 android studio 上开发 webview 应用程序。
问题是我的应用没有缓存超过 20.0MB。
我从 Settings>ApplicationManager>MyWebviewApp>Cache 检查了它。
当缓存达到超过 20.0MB 时,旧缓存似乎已被删除。
我可以增加缓存容量吗?
【问题讨论】:
标签: android webview browser-cache
尝试这样解决您的问题。 Source
WebView wvSubscribeNow = (WebView) findViewById(R.id.your_wv);
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
//noinspection deprecation
wvSubscribeNow.getSettings().setAppCacheMaxSize(Long.MAX_VALUE);
}
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
//noinspection deprecation
wvSubscribeNow.getSettings().setEnableSmoothTransition(true);
}
if (android.os.Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) {
wvSubscribeNow.getSettings().setMediaPlaybackRequiresUserGesture(true);
}
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
wvSubscribeNow.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);
}
wvSubscribeNow.getSettings().setAppCachePath(getApplicationContext().getCacheDir().getAbsolutePath());
wvSubscribeNow.getSettings().setAllowFileAccess(true);
wvSubscribeNow.getSettings().setAppCacheEnabled(true);
wvSubscribeNow.getSettings().setJavaScriptEnabled(true);
wvSubscribeNow.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
【讨论】:
setAppCacheMaxSize 在 API 级别 18 中已被弃用...?
试试这个希望它有效。
WebView view = (WebView) findViewById(R.id.vWeb);
view.getSettings().setAppCacheEnabled(true);
view.getSettings().setAppCacheMaxSize(1024 * 1024 * 10);
【讨论】: