Load local html file in webview android

末鹿安然 提交于 2020-01-01 05:20:14

问题


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.

The html file are under 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 :

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

回答1:


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

So move

src/main/assests/index.html

to

assets/index.html

In an Android Studio project use this folder:

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


来源:https://stackoverflow.com/questions/21557804/load-local-html-file-in-webview-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!