get the webArchive that by webview saveWebArchive

3.1k Views Asked by At

Android's WebView has this saveWebArchive method since API level 11:

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 try use WebArchiveReader who tell me can display the webArchive, but it just parse xml and i get the same result.

How can i do now?

2

There are 2 best solutions below

1
On

You can load the saved archive into your webView by first reading the archive into a String and then calling:

webView.loadDataWithBaseURL(null, archiveString, "application/x-webarchive-xml", "UTF-8", null);
0
On

Simple version:

loadUrl with file:// scheme

mWebView.loadUrl("file://"+getContext().getFilesDir().getPath()+"/example.mhtml"); 
//file:///data/user/0/{your app package name}/files/example.mhtml

"Handle the content by yourself" version:

  1. read every line of your mhtml file to a string (remember to add \r\n after each readLine())

  2. mWebView.loadDataWithBaseURL(/*source url, or even https://example.com/, don't put null or empty string*/, dataString, "multipart/related", "utf-8","");