Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Enabling HTML5 AppCache in Android Webview programatically.
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);
}
@shichaosong

This comment has been minimized.

Show comment Hide comment
@shichaosong

shichaosong Jul 16, 2013

thanks.

thanks.

@avinashgardas

This comment has been minimized.

Show comment Hide comment
@avinashgardas

avinashgardas Jan 21, 2015

Thanks a lot 😄

Thanks a lot 😄

@EpocDotFr

This comment has been minimized.

Show comment Hide comment
@EpocDotFr

EpocDotFr Feb 20, 2015

Or you can simply:

File dir = getCacheDir();

if (!dir.exists()) {
  dir.mkdirs();
}

webView.getSettings().setAppCachePath(dir.getPath());

Or you can simply:

File dir = getCacheDir();

if (!dir.exists()) {
  dir.mkdirs();
}

webView.getSettings().setAppCachePath(dir.getPath());
@salahzidi

This comment has been minimized.

Show comment Hide comment
@salahzidi

salahzidi Jun 24, 2015

Does this work, even if the appCache is not set in the html5 app?

Does this work, even if the appCache is not set in the html5 app?

@sud007

This comment has been minimized.

Show comment Hide comment
@sud007

sud007 Nov 6, 2015

doesn't make any difference for my usage. Still issues with HTML5 page load.

sud007 commented Nov 6, 2015

doesn't make any difference for my usage. Still issues with HTML5 page load.

@pkhutal

This comment has been minimized.

Show comment Hide comment
@pkhutal

pkhutal Nov 16, 2015

Hello,
I am trying to call webpage in webview with feature that if user is already login it should redirect to website else on the login page.but this thing work fine on browser but not on webview .please suggest me for this.
Thank you.

pkhutal commented Nov 16, 2015

Hello,
I am trying to call webpage in webview with feature that if user is already login it should redirect to website else on the login page.but this thing work fine on browser but not on webview .please suggest me for this.
Thank you.

@Enlac

This comment has been minimized.

Show comment Hide comment
@Enlac

Enlac Dec 7, 2015

@salahzidi it's programming not magic, of course you need to set it on your HTML5 file, it just allows webview to support it

Enlac commented Dec 7, 2015

@salahzidi it's programming not magic, of course you need to set it on your HTML5 file, it just allows webview to support it

@ArcherBG

This comment has been minimized.

Show comment Hide comment
@ArcherBG

ArcherBG Apr 11, 2016

You have to check for internet connectivity before telling to load from cache

You have to check for internet connectivity before telling to load from cache

@fabsk7

This comment has been minimized.

Show comment Hide comment
@fabsk7

fabsk7 Aug 22, 2016

thanks!!! a lot!

fabsk7 commented Aug 22, 2016

thanks!!! a lot!

@unclechen

This comment has been minimized.

Show comment Hide comment
@unclechen

unclechen May 9, 2017

Did anybody found that the Application Cache path is not in the path which setted in setAppCachePath funciton("/data/data/"+ getPackageName() +"/cache"),but it was in data/data/packagename/app_webview/Application Cache on Kitkat?

Did anybody found that the Application Cache path is not in the path which setted in setAppCachePath funciton("/data/data/"+ getPackageName() +"/cache"),but it was in data/data/packagename/app_webview/Application Cache on Kitkat?

@luck-apple

This comment has been minimized.

Show comment Hide comment
@luck-apple

luck-apple Jun 27, 2017

it works, thanks

it works, thanks

@yale8848

This comment has been minimized.

Show comment Hide comment
@yale8848

yale8848 Nov 3, 2017

appcache method will deprecated, you can look at this lib https://github.com/yale8848/CacheWebView , hope help you

yale8848 commented Nov 3, 2017

appcache method will deprecated, you can look at this lib https://github.com/yale8848/CacheWebView , hope help you

@ZealousMacwan

This comment has been minimized.

Show comment Hide comment
@ZealousMacwan

ZealousMacwan Apr 2, 2018

Thanks, It solved my issue. I was trying to show angular component inside webview but It was not showing, after I get to know that Angular requires html5 support, adding this code help me to solve issue

Thanks, It solved my issue. I was trying to show angular component inside webview but It was not showing, after I get to know that Angular requires html5 support, adding this code help me to solve issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment