8

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();

}
rahulritesh
  • 860
  • 1
  • 9
  • 17
  • Are you always seeing the error (in which case - what are you trying to load?) or do you only see it in offline mode? Also - unrelated, but why are you setting the layer type to software? That usually results in bad scrolling perf. – marcin.kosiba Nov 25 '13 at 17:33
  • The error happens only when the internet connection is turned off. And thanks for the advice, will try and see the difference. – rahulritesh Nov 26 '13 at 09:36
  • Have a look at the changes for Android 4.4 http://developer.android.com/about/versions/android-4.4.html#Behaviors – Srikanth Pai Nov 27 '13 at 06:52
  • Nothing that could explain the behavior that I am seeing in my app. – rahulritesh Dec 02 '13 at 06:47

2 Answers2

3

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.

rahulritesh
  • 860
  • 1
  • 9
  • 17
  • Do you have a reference to the Chromium bug? – ksasq Dec 17 '13 at 09:56
  • For the record: Judging by the name & date it seems to be [this issue](https://code.google.com/p/android/issues/detail?id=63501), closed due to lack of information. – benebun Apr 14 '15 at 17:01
0

Yes this is because of the following Issues :

http://code.google.com/p/android/issues/detail?id=63501 (need to be reopened! cc Rahulrit )

http://code.google.com/p/chromium/issues/detail?id=240043

Some Hacks:

1>if you are using your own server change the webserver implementation for MultipartCache such that images will stored as part by part, This is not the right solution give it a try!

2>Another solution is to save all the HTML file in in storage like this (not recommended )

// saving page from web to file 
File file = new File(this.getExternalFilesDir(null), "fileName.html");
FileUtils.copyURLToFile(new URL("http://www.bmimobile.co.uk/why-bmi.php"), file);
// loading saved file in webview
webview.loadUrl("file://" + file.getPath());

Credit: Nazar Merza

Note: All these solution are not 100% solution to above problem! we can cosider this as a temporary fix!

Community
  • 1
  • 1
LOG_TAG
  • 19,894
  • 12
  • 72
  • 105