Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving WebView page to cache

I have an app with WebView and I want the app to save the website the first time it is connected to the internet so that a further connection is no longer needed. I know some people are saving WebView pages to cache. I've done some research and I found some answers like this one.

But the problem is that I would need some example code on how to do this. Could someone give me an example on how to save a webpage .html file to external storage on Android?

This is the only code i've got at the moment to load a webpage.

//Connecting to UI elements
webView = (WebView) findViewById(R.id.webView1);

//Loading WebView URL
webView.loadUrl("https://www.easistent.com/urniki/izpis/263/16515/0/0/1");

I need some example code. I've seen a lot of documentation and guides, examples on this online but nothing I do works. I'd really appreciate a lot if someone gave me an example with comments.

like image 804
Guy Avatar asked Sep 04 '13 07:09

Guy


People also ask

How do I cache WebView?

This example demonstrate about How to enable app cache for webview in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

Can we cache WebView in android?

You can use the WebView cache to enable caching in WebView.

Where is WebView cache stored?

webview cache is saved into /data/data/[your_package_name]/cache/org.

Is WebView slower than browser?

Web View is slower on both as compared to native android browser.


1 Answers

You can directly use the WebView cache.

Typically, this is activated with the WebSettings.setCacheMode, using the mode LOAD_CACHE_ELSE_NETWORK

Use cached resources when they are available, even if they have expired.

Use like so :

webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);

However, in your case, the page declares a 'no-cache' option, which may prevent the WebView to store the page in cache altogether.

If you have the hand on the server side, you can also use the Application Caches API (see http://www.html5rocks.com/en/tutorials/appcache/beginner/ for more details on how that works)

like image 142
njzk2 Avatar answered Sep 27 '22 22:09

njzk2