Storing a WebView for offline browsing
In order to use caching on Android, you need to provide an HTML5 manifest file along with your HTML.
<!DOCTYPE html>
<html lang="en" manifest="manifest.file">
<head> ...
The caching mechanism will cache whatever entries you will list in the manifest file into an SQLite database. Keep in mind that the database is per app, not per webview. (I had a lot of problems with this).
This article explains all the steps you need to take to achieve this. http://web.archive.org/web/20140314001433/http://alex.tapmania.org/2010/11/html5-cache-android-webview.html
You can do a hello-world example for caching using this page: http://html5demos.com/offlineapp
You can save the cache on the SD card, as long as you add the WRITE_EXTERNAL_STORAGE permission to the application manifest.
Good luck!
Alex Kelly
Updated on July 20, 2022Comments
-
Alex Kelly almost 2 years
What would be the best way to force Android to store a webview to a media once the page is loaded?
A few more details:
Each page will be rich with images, scripts and styling.
Each page will be about 0.5 to 1 Megabyte.
They will be saved once the OnFinishedLoading Override is called.
It would be best to save it to the SD Card then use the local repo to open it back up.
Not sure if I should just use a sql database for the webviews, or take each view and do a write out to a file.
If I do the latter, what would be the easiest way so I don't have to write a full webcrawler to get each file linked?
-
Alex Kelly over 12 yearsI see, thanks - this makes sense. Now isn't there a 10 MB limit with this? I mean, I also saw in the source this : webView.getSettings().setAppCacheMaxSize(1024*1024*8); But, that still raises the question of how big can it be? Maybe just 10 mb per page? If thats the case, after it was cached, I'd just call the webview using websettings.Cacheonly right?
-
Daniel over 12 yearsIf the webview caching folder is set to the SD card, this will not impact your application cache folder. You can even call
webview.clearCache()
in order to check this, the database on the SD card will not be erased.