get the webArchive that by webview saveWebArchive

醉酒当歌 提交于 2019-12-11 01:09:59

问题


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:

  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","");



来源:https://stackoverflow.com/questions/18504404/get-the-webarchive-that-by-webview-savewebarchive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!