Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
166 views
in Technique[技术] by (71.8m points)

java - Open Local Html File in Webview - Android

I have a saved a file in the root folder and am trying to open it in a webview.

This is my code for saving:

    OutputStream outstream = null;
    outstream = openFileOutput(fileName ,MODE_WORLD_READABLE);

    /// if file the available for writing
    if (outstream != null) {
        /// prepare the file for writing
        OutputStreamWriter outputreader = new OutputStreamWriter(outstream);
        BufferedWriter buffwriter = new BufferedWriter(outputreader);

        /// write the result into the file
        buffwriter.write(result);
    }

    /// close the file
    outstream.close();

} catch (java.io.FileNotFoundException e) {
    System.out.println("File not found in the writing...");
} catch (IOException e) {
    System.out.println("In the writing...");
}

This is my code for recalling the file:

                    fileView.getSettings().setJavaScriptEnabled(true);
            fileView.loadUrl("file:///" + name); <---

and inside the app it gives me a file not found error.

Any insight is helpful.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
WebView mWebView=(WebView)findViewById(R.id.mWebView);

            mWebView.loadUrl("file:///book.html");
            mWebView.getSettings().setJavaScriptEnabled(true);
            mWebView.getSettings().setSaveFormData(true);
            mWebView.getSettings().setBuiltInZoomControls(true);
            mWebView.setWebViewClient(new MyWebViewClient());

private class MyWebViewClient extends WebViewClient 
{ 
    @Override 
    //show the web page in webview but not in web browser
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 
        view.loadUrl (url); 
        return true;
    }
}

try this


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...