Badr Hari
Badr Hari

Reputation: 8414

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?

Upvotes: 28

Views: 58492

Answers (5)

Dedetok
Dedetok

Reputation: 65

Use WebViewClientCompat and WebViewAssetLoader to load local asset html. WebViewClientCompat is part of androidx.webkit:webkit, you need to import it in build.gradle.kts (Module :app):

implementation("androidx.webkit:webkit:1.11.0")

Follow instruction from https://developer.android.com/develop/ui/views/layout/webapps/load-local-content. You need to create LocalContentWebViewClient.java, just copy the code from the instruction sample above.

To load your html into your webview:

WebView view = new WebView(this);
view.getSettings().setJavaScriptEnabled(true);
WebViewAssetLoader assetLoader = new WebViewAssetLoader.Builder()
         .addPathHandler("/assets/", new WebViewAssetLoader.AssetsPathHandler(this))
         .addPathHandler("/res/", new WebViewAssetLoader.ResourcesPathHandler(this))
         .build();
mWebView.setWebViewClient(new LocalContentWebViewClient(assetLoader));
view.loadUrl("file:///android_asset/hello.html");

Upvotes: 0

Quest Walnut
Quest Walnut

Reputation: 1

Since SDK 30, you need to call setAllowFileAccess(true) as well:

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

Upvotes: 0

Harry
Harry

Reputation: 937

Answer from Gugelhupf but with raw resource.
Advantage from this solution: you keep translation working!

WebView webView = findViewById(R.id.about_text);
try {
  InputStream inputStream = getResources().openRawResource(R.raw.about);
  BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
  StringBuilder stringBuilder = new StringBuilder();
  String line;
  while ((line = bufferedReader.readLine()) != null) {
    stringBuilder.append(line).append("\n");
  }
  webView.loadDataWithBaseURL(null, stringBuilder.toString(), "text/html", "UTF-8", null);
} catch (IOException e) {
  e.printStackTrace();
}

Upvotes: 0

Gugelhupf
Gugelhupf

Reputation: 970

Similar problem :

I use many productFlavors with different applicationId.

If I attempt to load a html file from res/raw/file.html I get a ClassNotFoundException Didn't find class "product.flavor.package.R$raw"

The R.java file has a different Package name.

It look's like you can't load a URL from file like : webView.loadUrl( "file:///android_res/raw/page.html"); because the WebView try to use the R.class file with has a different package name.

I assume the ERR_FILE_NOT_FOUND from loading a html file from assets has the same problem but you don't see the exception. ( webView.loadUrl( "file:///android_assets/page.html" ); )

With this small work around I solve my problem :

try {
        AssetManager assetManager = context.getAssets();
        InputStream stream = assetManager.open("page.html");
        BufferedReader r = new BufferedReader(new InputStreamReader(stream));
        StringBuilder total = new StringBuilder();
        String line;
        while ((line = r.readLine()) != null) {
            total.append(line).append("\n");
        }
        webView.loadDataWithBaseURL(null, total.toString(), "text/html", "UTF-8", null);
    } catch (Exception xxx) {
        Log.e(TAG, "Load assets/page.html", xxx);
    }

I hope this helps. Stephan

Upvotes: 11

Rustam
Rustam

Reputation: 6515

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

Upvotes: 61

Related Questions