简体   繁体   English

下载Web内容并在运行时加载Android webview

[英]Download web content and load on Android webview at runtime

I want to load a local web content on a webview. 我想在webview上加载本地 Web内容。 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/ ). 我正在下载一个zip文件,解压缩并保存内部存储( /data/data/<app>/files/ )上的文件(main.html和资源文件 - css,js,fonts,pngs等)。

All files are in the same directory: 所有文件都在同一目录中:

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

So, the html file points resource files as follows: 因此,html文件指向资源文件,如下所示:
<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: 但是webview在棒棒糖前设备中显示file not found错误,并且在棒棒糖设备中显示:

"The webpage at file:///data/data/<app>/files/main.html could not be loaded because: net::ERR_ACCESS_DENIED" “无法加载file:///data/data/<app>/files/main.html的网页,因为:net :: ERR_ACCESS_DENIED”

If I load the html as a string by running 如果我通过运行将html加载为字符串

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? 有谁知道如何在Android中运行时存储的webview上加载本地文件?

Thanks 谢谢

I forgot the config methods of the webview. 我忘记了webview的配置方法。

webview.getSettings().setAllowFileAccess(false)

This was the problem. 这是问题所在。 By turning to true the webview is able to load local files. 通过转到true ,webview可以加载本地文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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