Android's WebView has this saveWebArchive method since API level 11: http://developer.android.com/.
It can save entire websites as webarchives, which is great! But how do I get the downloaded contents back into a webview? I tried
webview.loadUrl(Uri.fromFile(mywebarchivefile));
But that only displays xml on the screen.
I've found an undocumented way of reading saved webarchive. Just do:
and then call
The reference: http://androidxref.com/4.0.4/xref/external/webkit/Source/WebCore/loader/archive/ArchiveFactory.cpp
Available from Android 3.0, api level 11.
Update Feb. 21, 2014
My answer posted below does not apply to web archive files saved under Android 4.4 KitKat and newer. The saveWebArchive() method of WebView under Android 4.4 "KitKat" (and probably newer versions too) does not save the web archive in XML code that this reader code posted below. Instead it saves pages in MHT (MHTML) format. It is easy to read back the .mht files - just use:
That's all, much easier than the previous method, and compatible with other platforms.
Previously posted
I needed it myself, and everywhere I searched, there were unanswered questions like this. So I had to work it out myself. Below is my little WebArchiveReader class and sample code on how to use it. Please note that despite the Android docs declaring that shouldInterceptRequest() was added to WebViewClient in API11 (Honeycomb), this code works and was tested successfully in Android emulators down to API8 (Froyo). Below is all the code that's needed, I also uploaded the full project to GitHub repository at https://github.com/gregko/WebArchiveReader
File WebArchiveReader.java:
Here is how to use this class, sample MyActivity.java class:
To make things complete, here is my little Lt.java class for debug output:
Hope this is helpful.
Update July 19, 2013
Some web pages don't have meta tag specifying text encoding, and then the code we show above does not display the characters correctly. In the GitHub version of this code I now added charset detection algorithm, which guesses the encoding in such cases. Again, see https://github.com/gregko/WebArchiveReader
Greg