Antarix
Antarix

Reputation: 685

How to save a complete webpage and load later for offline access

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.

Upvotes: 3

Views: 7916

Answers (2)

ohaleck
ohaleck

Reputation: 671

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.

Upvotes: 1

duggu
duggu

Reputation: 38439

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"/>

Upvotes: 1

Related Questions