27

On Android devices up to and including 4.4.2, the default Browser and Chrome support the HTML5 cache manifest. However, on those same devices, the WebView component does not seem to support the HTML5 cache manifest. Does anybody know how I can make the WebView component support the HTML5 manifest?

Rishabh Srivastava
  • 3,683
  • 2
  • 30
  • 58
Lars D
  • 8,483
  • 7
  • 34
  • 37
  • 4
    I found the result here: http://alex.tapmania.org/?p=110 – Lars D Jan 09 '11 at 19:47
  • Probably the most non-intuitive aspect I took away from the posted solution is that you have to manually set the appCachePath of the WebView, or it won't work. You won't know why it didn't work. It just won't. – KevinH Feb 27 '12 at 16:43
  • 2
    I really don't get where this is too localized – RMalke Apr 29 '13 at 16:09
  • 3
    I just encountered this issue. It would be good to have an approved answer - I disagree with the closure. – Greg Ball Nov 27 '13 at 01:38
  • If I remember correctly (as this was a while ago), you should also make sure the file path you set also exists. It appeared that some devices worked when the folder didn't exist and some devices did work (that may have also been caused by different versions of Android). Just a thought. – Randy Dec 30 '14 at 13:51
  • Please have a look at the answer of a similar question. http://stackoverflow.com/a/11684044/3181595 – frogEye Apr 21 '15 at 11:24

3 Answers3

1
webView.getSettings().setDomStorageEnabled(true);

// Set cache size to 8 mb by default. should be more than enough
webView.getSettings().setAppCacheMaxSize(1024*1024*8);

// This next one is crazy. It's the DEFAULT location for your app's cache
// But it didn't work for me without this line
webView.getSettings().setAppCachePath("/data/data/"+ getPackageName() +"/cache");
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setAppCacheEnabled(true);

webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
Aides
  • 3,643
  • 5
  • 23
  • 39
0

Try this code:

private void enableHTML5AppCache() {

          webView.getSettings().setDomStorageEnabled(true);

          // Set cache size to 8 mb by default. should be more than enough
          webView.getSettings().setAppCacheMaxSize(1024*1024*8);

          // This next one is crazy. It's the DEFAULT location for your app's cache
          // But it didn't work for me without this line
          webView.getSettings().setAppCachePath("/data/data/"+ getPackageName() +"/cache");
          webView.getSettings().setAllowFileAccess(true);
          webView.getSettings().setAppCacheEnabled(true);

          webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
    }

Here the link.

Stefano
  • 389
  • 2
  • 15
0
@Override
 public void onReceivedError(WebView view, int errorCode,
      String description, String failingUrl)
  {
    // The magic redirect
    if( "http://HTML5app.com/app/".equals(failingUrl) ) {
      // main.html is the place we are redirected to by the server if we are online
      mWebView.loadUrl("http://HTML5app.com/app/main.html");

     return;
   }
   else if( "http://HTML5app.com/app/main.html".equals(failingUrl) ) {
     // The cache failed - We don't have an offline version to show
     // This code removes the ugly android's "can't open page"
     // and simply shows a dialog stating we have no network
     view.loadData("", "text/html", "UTF-8");
     showDialog(DIALOG_NONETWORK);
   }
 }

Above method will be used to handle redirection in offline scenario. [For implementing appcache and path refer previous comment.

Reference Link : HTML5 Cache mechanism in android

Srini
  • 348
  • 1
  • 2
  • 17