3

I have WebView and wanted to save some web pages to internal or external storage for offline access. Please provide some example if someone has already gone through this.

Antarix
  • 665
  • 1
  • 10
  • 29
  • Hey .. how did you solve this query.. I have to implement something similar to this .your suggestion would be great . – Nibha Jain Oct 11 '13 at 13:05
  • well thanks to this guy https://github.com/gregko/WebArchiveReader for his library which helped me to load `WebArchive` saved as `xml` – Antarix Oct 12 '13 at 06:07

2 Answers2

1

Try below code for achieve your task and check this link :-

WebView webView = new WebView( context );
webView.getSettings().setAppCacheMaxSize( 5 * 1024 * 1024 ); // 5MB
webView.getSettings().setAppCachePath( getApplicationContext().getCacheDir().getAbsolutePath() );
webView.getSettings().setAllowFileAccess( true );
webView.getSettings().setAppCacheEnabled( true );
webView.getSettings().setJavaScriptEnabled( true );
webView.getSettings().setCacheMode( WebSettings.LOAD_DEFAULT ); // load online by default

if ( !isNetworkAvailable() ) { // loading offline
    webView.getSettings().setCacheMode( WebSettings.LOAD_CACHE_ELSE_NETWORK );
}

webView.loadUrl( "http://www.google.com" );

permission which require

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
Community
  • 1
  • 1
duggu
  • 37,851
  • 12
  • 116
  • 113
  • I appreciated your answer but what i actually want is i want to give option to save that webpage and show in details in `ListView` like title ,for more details see the offline functionality of this app https://play.google.com/store/apps/details?id=com.world.newspapers – Antarix May 08 '13 at 04:35
1

If you have access to the server-side, the best idea would be to use HTML5 cache manifest, as I described it here: https://stackoverflow.com/a/14348234/737885

If you don't, you can try simply caching it as HCD suggested, but that solution depends on HTTP headers, and other considerations, so it might not be bullet-proof.

An alternative solution would be to point the WebView to the URLs comprising the site by means of the WebViewClient callbacks (see http://developer.android.com/reference/android/webkit/WebViewClient.html) and downloading each resource manually using HttpClient. This is the only solution of the three, which creates a re-usable offline copy of the site. The other are based on the WebView cache so you can only use the cached copies inside the very same WebView.

Community
  • 1
  • 1
ohaleck
  • 661
  • 4
  • 20
  • The cache manifest is now deprecated, and it no longer works in some browsers. You can do this using a [Service Worker](https://deanhume.com/create-a-really-really-simple-offline-page-using-service-workers/) instead. – Anderson Green Jul 06 '21 at 02:54