简体   繁体   中英

Download web content and load on Android webview at runtime

I want to load a local web content on a webview. I'm downloading a zip file, unzipping it and saving the files (main.html and resource files - css, js, fonts, pngs, etc) on internal storage ( /data/data/<app>/files/ ).

All files are in the same directory:

|- main.html
|- file.js
|- ...

So, the html file points resource files as follows:
<script type="text/javascript" src="file.js"></script>

I though this would work

String path = context.getFilesDir() + File.separator + "main.html";
webview.loadUrl("file://" + path)

but the webview shows file not found error in pre lollipop devices, and in lollipop devices shows:

"The webpage at file:///data/data/<app>/files/main.html could not be loaded because: net::ERR_ACCESS_DENIED"

If I load the html as a string by running

String file = htmlFile.fileToString();
webview.loadDataWithBaseURL("", file, "text/html", "utf-8", "");

it works, but I have to resolve the dependencies by providing the full path of all resource files. There must be a better way to deal with this.

Does anyone know how to load a local file on a webview that was stored at runtime in Android?

Thanks

I forgot the config methods of the webview.

webview.getSettings().setAllowFileAccess(false)

This was the problem. By turning to true the webview is able to load local files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM