在Android中,如何在Webview中打开本地HTML文件?

14
我有一个在根目录下保存的文件,并尝试在Webview中打开它。
这是我的保存代码:
    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...");
}

这是我用来调用文件的代码:

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

在应用程序内部会提示“文件未找到”错误。

任何见解都有帮助。

6个回答

18
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;
    }
}

试一下这个


这也有避免在API>26中暴露URIs崩溃的好处。 - Luis A. Florit

13

实际上,当你使用 file:///... 打开一个 URL 时, 这意味着你应该将该文件保存在资产目录下(比如说 test.html)。 现在假设你需要访问 test.html 文件,你需要像这样编写:

loadURL("file:///android_asset/test.html');

我想从test.html导航到page1.html,因为它们都在同一个目录下,所以我在test.html中使用<a href="page1.html">Page 1</a>,但是不起作用。 - Sarz

9
路径错误,假设未触发异常。
file:/// 告诉浏览器查找 /name。
openFileOutput(fileName) 告诉应用程序在 <application-files-directory>/fileName 中写入。
你的 url 应该是 "file:///"+getFilesDir()+File.separator+fileName。

我正在考虑开始制作Android游戏。理论上,我是否可以创建一个HTML5画布和JavaScript游戏,将文件存储在Android手机上,然后使用WebView打开它们,让玩家玩游戏? - Jacob

0
对于将与应用程序捆绑的文件,您可以通过在项目资源管理器中右键单击应用程序,然后选择新建=>文件夹=>资产文件夹来添加“资产”文件夹到您的项目中。
将HTML文件添加到您的资产文件夹中,然后通过以下方式加载它:
fileView.loadUrl("file:///android_asset/"+name);

相同的URL可以用于在您的HTML中链接到其他HTML或CSS文件。


0

0

您可以先读取资产文件,然后通过asd将其显示在Webview上,就像这样

        BufferedReader read = null; 
StringBuilder data = new StringBuilder(); 
try {
            read = new BufferedReader(new InputStreamReader(getAssets().open("htmlFile.html"), "UTF-8")); 
            String webData;
            while ((mLine = read.readLine()) != null) {
               data.append(mline);
            } 
    } catch (IOException e) {
             log(",e.getmessage()) } finally {
            if (reader != null) {
                 try {
                     reader.close();
                 } catch (IOException e) {
                     log(",e.getmessage())
                 }
            } 
    }

然后在webview中加载这些数据

webview.loadData(data, "text/html", "UTF-8");

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接