问题
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?
回答1:
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);
回答2:
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:
read every line of your mhtml file to a string (remember to add \r\n after each readLine())
mWebView.loadDataWithBaseURL(/*source url, or even https://example.com/, don't put null or empty string*/, dataString, "multipart/related", "utf-8","");
来源:https://stackoverflow.com/questions/18504404/get-the-webarchive-that-by-webview-savewebarchive