Allow access to local assets in android

292 views Asked by At

I want to have my webview refer to a site, say example.com. In the assets directory for the app, I will include all the static assets such as javascript, styles and the images.

However, when I use the following code on my website: <link rel="stylesheet" href="file:///android_asset/css/main.css"> But that just gives me an error.

Any ideas on how to do it?

1

There are 1 answers

0
Alex Forcier On

This won't work directly, due to JavaScript's same-origin policy:

https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy

A possible workaround would be to process the remote site HTML as a String, and then load the HTML string using loadDataWithBaseURL() with the android-asset base URL:

webView.loadDataWithBaseURL("file:///android-asset/", remoteHtmlString, "text/html", "UTF-8", null);