javaandroidwebviewandroid-studiogradle

Loading html file to webview on android from assets folder using Android Studio


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?


Solution

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