在webview android中加载本地html文件 [英] Load local html file in webview android
问题描述
我正在尝试将html文件的内容加载到android的webview中.但是,它给了我网页不可用错误".如果我尝试使用诸如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屋!