【问题标题】:get the webArchive that by webview saveWebArchive通过 webview saveWebArchive 获取 webArchive
【发布时间】:2013-09-01 12:01:08
【问题描述】:

Android 的 WebView 从 API 级别 11 开始就有这个 saveWebArchive 方法:

它可以将整个网站保存为网络档案,这很棒!但是如何将下载的内容恢复到 webview 中?我试过了

webview.loadUrl(Uri.fromFile(mywebarchivefile)); 但这只会在屏幕上显示 xml。

我尝试使用 WebArchiveReader,他告诉我可以显示 webArchive,但它只是解析 xml,我得到相同的结果。

我现在该怎么办?

【问题讨论】:

    标签: android webarchive


    【解决方案1】:

    您可以先通过reading the archive into a String 将保存的存档加载到您的 webView 中,然后调用:

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

    【讨论】:

    • 为什么要在第一个参数中传递 null
    【解决方案2】:

    简单版:

    loadUrl 与 file:// 方案

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

    “自己处理内容”版本:

    1. 将 mhtml 文件的每一行读取为一个字符串(记得在每个 readLine() 之后添加 \r\n)

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

    【讨论】:

      猜你喜欢
      • 2012-10-04
      • 2019-01-31
      • 1970-01-01
      • 1970-01-01
      • 2018-01-30
      • 2019-08-15
      • 1970-01-01
      • 1970-01-01
      • 2016-11-25
      相关资源
      最近更新 更多