简体   繁体   中英

WebView Cache not working in Android 4.4

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.

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM