在webview android中加载本地html文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21557804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:45:19  来源:igfitidea点击:

Load local html file in webview android

androidhtmlwebviewandroid-studiolocal

提问by user782400

I am trying to load the contents of a html file in a webview in android. However, it gives me the "Webpage not available error". If I try websites such as google or yahoo, they work.

我正在尝试在 android 的 webview 中加载 html 文件的内容。但是,它给了我“网页不可用错误”。如果我尝试使用 google 或 yahoo 等网站,它们会起作用。

The html file are under src > main > assests > index.html

html 文件位于 src > main >assests > index.html

Can someone help me with this issue. Thank You.

有人可以帮我解决这个问题。谢谢你。

Following is my code :

以下是我的代码:

setContentView(R.layout.activity_main);

WebView mWebView;
mWebView = (WebView) findViewById(R.id.activity_main_webview);

WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);

mWebView.loadUrl("file:///android_asset/index.html");

XML File :

XML文件:

<WebView
    android:id="@+id/activity_main_webview"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

采纳答案by jimmithy

The html file should be placed in the assetsfolder (note the spelling), which will belong in the root directory of your project.

html 文件应放在assets文件夹中(注意拼写),该文件夹将位于项目的根目录中。

So move

所以动

src/main/assests/index.html

to

assets/index.html

In an Android Studio project use this folder:

在 Android Studio 项目中使用此文件夹:

/app/src/main/assets/index.html