I have been working on an application that caches an image from the web and shows it even when the user is offline. This worked pretty well until Android 4.4 came out. Now, all I see is a "Can not load the webpage" error. I suppose it might have to do something with the fact that Kitkat uses Chromium for loading webviews, but I am not very sure. Any fixes for this?
Here is my code:
mWebView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
mWebView.setWebViewClient(new MyWebViewClient());
mWebViewSettings = mWebView.getSettings();
mWebViewSettings.setJavaScriptEnabled(true);
mWebViewSettings.setAllowFileAccess(true);
mWebViewSettings.setAppCacheEnabled(true);
if (Build.VERSION.SDK_INT < 18)
mWebViewSettings.setAppCacheMaxSize(5 * 1024 * 1024); // 5MB
mWebViewSettings.setLoadsImagesAutomatically(true);
mWebViewSettings.setAppCachePath(getActivity().getApplicationContext()
.getCacheDir().getAbsolutePath());
mWebViewSettings.setBuiltInZoomControls(true);
if(Build.VERSION.SDK_INT<=18)
mWebViewSettings.setDefaultZoom((WebSettings.ZoomDensity.FAR));
mWebViewSettings.setUseWideViewPort(true);
mWebViewSettings.setLoadWithOverviewMode(true);
I am loading it using the following code:
if (NetworkUtil.IS_ONLINE_WITH_DATA_CONNECTION) {
MyPrefs = getActivity().getSharedPreferences(
"AHSelectionPreffs",
Context.MODE_PRIVATE);
Date CAMPUS_MAP_LAST_UPDATE_DATE = new Date();
String CAMPUS_MAP_LAST_UPDATE_DATE_STRING = MyPrefs
.getString("CAMPUS_MAP" + String.valueOf(StorageHelper.ID), null);
SimpleDateFormat simpleDateFormatter = new SimpleDateFormat(
"EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
if (CAMPUS_MAP_LAST_UPDATE_DATE_STRING != null) {
try {
CAMPUS_MAP_LAST_UPDATE_DATE = simpleDateFormatter
.parse(CAMPUS_MAP_LAST_UPDATE_DATE_STRING);
} catch (ParseException e1) {
e1.printStackTrace();
System.out.println("Cant format date!!!!");
}
int n = (int) ((curDate.getTime() - CAMPUS_MAP_LAST_UPDATE_DATE
.getTime()) / (1000 * 60 * 60));
if (n < 24 * UPDATE_DURATION_IN_DAYS) {
mWebView.getSettings().setCacheMode(
WebSettings.LOAD_CACHE_ELSE_NETWORK);
} else {
updateCampusMapData();
}
} else {
updateCampusMapData();
}
} else {
mWebView.getSettings().setCacheMode(
WebSettings.LOAD_CACHE_ELSE_NETWORK);
}
if (StorageHelper.campus_map_link.Mobile_Link_URL != null) {
mWebView.loadUrl(StorageHelper.campus_map_link.Mobile_Link_URL);
}
private void updateCampusMapData() {
mWebView.getSettings().setCacheMode(
WebSettings.LOAD_NO_CACHE);
MyPrefs = getActivity().getSharedPreferences(
"AHSelectionPreffs", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = MyPrefs.edit();
editor.putString("CAMPUS_MAP" + String.valueOf(StorageHelper.ID),
curDate.toString());
editor.commit();
}
Got it solved myself. I figured out that Chromium is not caching images with size greater than 1 MB. In other cases, it works perfectly fine. Reported this to Google, for now.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With