【问题标题】:How can I get a webview archive file to InputStream in android?如何在 android 中将 webview 存档文件获取到 InputStream?
【发布时间】:2013-01-07 13:49:09
【问题描述】:

我之前问过一个类似的问题,关于将存档文件加载到 WebView。

根据tutorial,我必须将存档文件加载到InputStream,以便我可以使用WebArchiveReader.java中的方法loadToWebView。

但是,在tutorial 中,作者通过使用以下语句获取存档文件:

InputStream is = getAssets().open("TestHtmlArchive.xml");

不幸的是,我想从“资产”文件夹以外的其他地方获取存档文件。

WebView webView = (WebView) rootView.findViewById(R.id.webview_layout);
String url = "http://www.yahoo.com";
webView.loadUrl(url);
String path = getFilesDir().getAbsolutePath() + File.separator + "yahoo" + ".html";
webView.saveWebArchive(path);
webView.loadUrl("file://" + path);

假设我想加载上面代码中保存的文件“yahoo.html”。

我怎样才能把它放到 InputStream 的一个实例中?

【问题讨论】:

    标签: android android-webview inputstream webarchive


    【解决方案1】:

    似乎向 InputStream 打开文件的正确方法如下所示。

                String path = getFilesDir().getAbsolutePath() + File.separator
                        + "yahoo" + ".html";
    
                File file = new File(path);
    
                try {
                    InputStream is = new BufferedInputStream(new FileInputStream(file));
    
                    WebArchiveReader wr = new WebArchiveReader() {
    
                        @Override
                        public void onFinished(WebView webView) {
                            System.out.println("Page loaded");
                        }
                    };
    
                    if (wr.readWebArchive(is)) {
                        wr.loadToWebView(webView);
                    }
    
                } catch (IOException e) {
                    e.printStackTrace();
                }
    

    【讨论】:

      猜你喜欢
      • 2020-05-15
      • 2021-12-09
      • 1970-01-01
      • 2011-05-09
      • 1970-01-01
      • 1970-01-01
      • 2018-11-19
      • 1970-01-01
      相关资源
      最近更新 更多