使用Android Studio从assets文件夹将html文件加载到android上的webview

使用Android Studio从assets文件夹将html文件加载到android上的webview

问题描述:

我使用的是 Android Studio/Gradle.

I'm using Android Studio/Gradle.

app\src\main\android_asset 文件夹中有名为 chart.html.. 的文件.

app\src\main\android_asset folder has the file called chart.html..

我正在尝试将这个文件加载到我的 webview 中:

I'm trying to load this file to my webview like this:

WebView view = new WebView(this);
view.getSettings().setJavaScriptEnabled(true);
view.loadUrl("file:///android_asset/chart.html");
setContentView(view);

但我总是收到错误:无法加载,因为 ERR_FILE_NOT_FOUND.

But I always get the error: could not be loaded because ERR_FILE_NOT_FOUND.

我在这里遗漏了什么?

目录名应该是 assets 而不是 android_assets

The directory name should be assets not android_assets

这样做:

如上图所示,只需右键单击您的应用程序->新建->文件夹->资产文件夹

As shown in the above pics just right click on your app->New->Folder->Assets Folder

现在将您的 .html 文件放在 assets 文件夹中.

Now put your .html file here in assets folder.

就是这样.完成.

代码中的其余部分与您所做的相同.

Remaining is same in code what you did.

WebView view = new WebView(this);
view.getSettings().setJavaScriptEnabled(true);
view.loadUrl("file:///android_asset/hello.html");
setContentView(view);