androidandroid-webviewinputstreamwebarchive

How can I get a webview archive file to InputStream in android?


I asked a similar question before about the loading archive file to WebView.

According to the tutorial, I have to load the archive file to InputStream so that I can use the method loadToWebView in WebArchiveReader.java.

However, in the tutorial, the writer get the archive file by using the following statement:

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

Unfortunately, I want to get the archive file from somewhere else other than "Assets" folder.

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

Assuming I want to load the file "yahoo.html" as I saved in the code above.

How can I get it to an instance of InputStream?


Solution

  • it seems the right way to open a file to InputStream is shown as follow.

                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();
                }