I have Web Application runs in Android. I can cache my web, so if user has not internet connection, he can still access web from cache. But it only run when user has not Internet Connection.
我在Android中运行Web应用程序。我可以缓存我的网页,所以如果用户没有网络连接,他仍然可以从缓存中访问网页。但它只在用户没有Internet连接时运行。
Now, for optimzing my apps, when user has internet connection, I want to cache all image that show in WebView
And store it locally/cache it, and when user open a page with same src image that I have cached it, it load from local, not load it from web again. Or May be not image, but html
page that visited cache it, and when user back to url page again, Webview
load it from local, not from internet.
现在,为了优化我的应用程序,当用户有互联网连接时,我想要缓存在WebView中显示的所有图像并将其存储在本地/缓存它,当用户打开一个具有相同src图像的页面时,我已缓存它,它从本地,不再从网络加载它。或者可能不是图像,但访问缓存它的html页面,当用户再次回到url页面时,Webview从本地加载,而不是从Internet加载。
I have search the code for two days but still no finding solution.
我已经搜索了两天的代码,但仍然找不到解决方案。
Thanks for help.
感谢帮助。
2 个解决方案
#1
5
Try this
尝试这个
private void enableWVCache() {
webView.getSettings().setDomStorageEnabled(true);
// Set cache size to 8 mb by default. should be more than enough
webView.getSettings().setAppCacheMaxSize(1024*1024*8);
File dir = getCacheDir();
if (!dir.exists()) {
dir.mkdirs();
}
webView.getSettings().setAppCachePath(dir.getPath());
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
}
and then
接着
if ( !isNetworkAvailable() ) { // loading offline
webView.getSettings().setCacheMode( WebSettings.LOAD_CACHE_ELSE_NETWORK );
}
}
and the method isNetworkAvailable()
checks for an active network connection:
并且方法isNetworkAvailable()检查活动的网络连接:
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService( CONNECTIVITY_SERVICE );
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
Finally, don't forget to add the following three permissions to your AndroidManifest.xml
:
最后,不要忘记将以下三个权限添加到AndroidManifest.xml:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
#2
0
tencent QQ team develop a H5 framework,you can use it for speeping up your web load.Here is github address:https://github.com/Tencent/VasSonic
腾讯QQ团队开发了一个H5框架,你可以用它来加载你的网页加载。这里是github地址:https://github.com/Tencent/VasSonic
#1
5
Try this
尝试这个
private void enableWVCache() {
webView.getSettings().setDomStorageEnabled(true);
// Set cache size to 8 mb by default. should be more than enough
webView.getSettings().setAppCacheMaxSize(1024*1024*8);
File dir = getCacheDir();
if (!dir.exists()) {
dir.mkdirs();
}
webView.getSettings().setAppCachePath(dir.getPath());
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
}
and then
接着
if ( !isNetworkAvailable() ) { // loading offline
webView.getSettings().setCacheMode( WebSettings.LOAD_CACHE_ELSE_NETWORK );
}
}
and the method isNetworkAvailable()
checks for an active network connection:
并且方法isNetworkAvailable()检查活动的网络连接:
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService( CONNECTIVITY_SERVICE );
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
Finally, don't forget to add the following three permissions to your AndroidManifest.xml
:
最后,不要忘记将以下三个权限添加到AndroidManifest.xml:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
#2
0
tencent QQ team develop a H5 framework,you can use it for speeping up your web load.Here is github address:https://github.com/Tencent/VasSonic
腾讯QQ团队开发了一个H5框架,你可以用它来加载你的网页加载。这里是github地址:https://github.com/Tencent/VasSonic