在webview android中加载本地html文件 [英] Load local html file in webview android
问题描述
我正在尝试在 android 的 webview 中加载 html 文件的内容.但是,它给了我网页不可用错误".如果我尝试使用 google 或 yahoo 等网站,它们会起作用.
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.
html 文件在 src > main >assests > index.html
The html file are under src > main > assests > index.html
有人能帮我解决这个问题吗?谢谢.
Can someone help me with this issue. Thank You.
以下是我的代码:
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 文件:
<WebView
android:id="@+id/activity_main_webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
推荐答案
html 文件应该放在 assets
文件夹中(注意拼写),该文件夹属于你的项目根目录.
The html file should be placed in the assets
folder (note the spelling), which will belong in the root directory of your project.
所以动起来
src/main/assests/index.html
到
assets/index.html
在 Android Studio 项目中使用此文件夹:
In an Android Studio project use this folder:
/app/src/main/assets/index.html
这篇关于在webview android中加载本地html文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!