Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading html file to webview on android from assets folder

I'm using Android Studio/Gradle.

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

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);

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

What am I missing here?

like image 870
Badr Hari Avatar asked Sep 06 '25 14:09

Badr Hari


1 Answers

The directory name should be assets not android_assets

Do like this: enter image description here

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

Now put your .html file here in assets folder.

That's it. Done.

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);
like image 164
Rustam Avatar answered Sep 08 '25 06:09

Rustam