用 android WebView 加载现有的.html 文件

我确实尝试了样品,演示从谷歌代码和其他资源与 WebView,但当我尝试在我自己的代码,它不适合我。

我想加载 myfile.html,我把资产文件夹,并使用:

private WebView myWebView;


myWebView.loadUrl("file:///android_assets/myfile.html");

在模拟器上显示错误

file:///android_assets/myfile.html的网页不能 未找到所请求的文件。 /android_assets/myfile.html

当我把文件放入 res/raw/文件夹并使用:

myWebView.loadUrl("file:///android_res/raw/myfile.html");

那么可能只有仿真器 android 2.2 API 级别8可以加载该文件,其他老版本显示相同的错误。我错过了什么吗?

是否有任何方法加载现有的。在所有 API 版本上工作的应用程序包中的 html 文件?

148285 次浏览

You could read the html file manually and then use loadData or loadDataWithBaseUrl methods of WebView to show it.

ok, that was my very stupid mistake. I post the answer here just in case someone has the same problem.

The correct path for files stored in assets folder is file:///android_asset/* (with no "s" for assets folder which i was always thinking it must have a "s").

And, mWebView.loadUrl("file:///android_asset/myfile.html"); works under all API levels.

I still not figure out why mWebView.loadUrl("file:///android_res/raw/myfile.html"); works only on API level 8. But it doesn't matter now.

paste your .html file in assets folder of your project folder. and create an xml file in layout folder with the fol code: my.xml:

<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>

add fol code in activity

setContentView(R.layout.my);
WebView mWebView = null;
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("file:///android_asset/new.html"); //new.html is html file name.

Copy and Paste Your .html file in the assets folder of your Project and add below code in your Activity on onCreate().

        WebView view = new WebView(this);
view.getSettings().setJavaScriptEnabled(true);
view.loadUrl("file:///android_asset/**YOUR FILE NAME**.html");
view.setBackgroundColor(Color.TRANSPARENT);
setContentView(view);

If your structure should be like this:

/assets/html/index.html

/assets/scripts/index.js

/assets/css/index.css

Then just do ( Android WebView: handling orientation changes )

    if(WebViewStateHolder.INSTANCE.getBundle() == null) { //this works only on single instance of webview, use a map with TAG if you need more
webView.loadUrl("file:///android_asset/html/index.html");
} else {
webView.restoreState(WebViewStateHolder.INSTANCE.getBundle());
}

Make sure you add

    WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
webSettings.setAllowFileAccessFromFileURLs(true);
webSettings.setAllowUniversalAccessFromFileURLs(true);
}

Then just use urls

<html>
<head>
<meta charset="utf-8">
<title>Zzzz</title>
<script src="../scripts/index.js"></script>
<link rel="stylesheet" type="text/css" href="../css/index.css">

The debug compilation is different from the release one, so:

Consider your Project file structure like that [this case if for a Debug assemble]:

src
|
debug
|
assets
|
index.html

You should call index.html into your WebView like:

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

So forth, for the Release assemble, it should be like:

src
|
release
|
assets
|
index.html

The bellow structure also works, for both compilations [debug and release]:

src
|
main
|
assets
|
index.html