将 HTML 文件加载到 WebView 中
我有一个本地 html 页面以及它指向的几个其他资源(css 文件和 Javascript 库),我想将它们加载到 WebView 中。如何才能实现这一目标?
也许这不是最好的方法,但我仍在尝试。
I have a local html page along with several other resources pointed by it (css files and Javascript libraries) that I would like to load into a WebView . How could this be achieved ?
Perhaps not the best way to procede but I'm still experimenting.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
最简单的方法可能是将您的 Web 资源放入 assets 文件夹,然后调用:
Java 和 Webview 之间的完整通信 查看此
更新:资产文件夹通常是以下文件夹:
<项目>/src/main/assets
可以在
.iml
文件 的资产文件夹配置设置中将其更改为:请参阅文章 Android 中资源文件夹的放置位置工作室
The easiest way would probably be to put your web resources into the assets folder then call:
For Complete Communication between Java and Webview See This
Update: The assets folder is usually the following folder:
<project>/src/main/assets
This can be changed in the asset folder configuration setting in your
<app>.iml
file as:<option name=”ASSETS_FOLDER_RELATIVE_PATH” value=”/src/main/assets” />
See Article Where to place the assets folder in Android Studio
也许这个示例可以帮助:
probably this sample could help:
在这种情况下,使用
WebView#loadDataWithBaseUrl()
比WebView#loadUrl()
更好!url: url/path 指向所有 JavaScript 文件和 html 链接都有其来源的目录的字符串。如果为空,则为:空白。
data:包含 hmtl 文件的字符串,例如使用 BufferedReader 读取
更多信息: WebView.loadDataWithBaseURL(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
In this case, using
WebView#loadDataWithBaseUrl()
is better thanWebView#loadUrl()
!url: url/path String pointing to the directory all your JavaScript files and html links have their origin. If null, it's about:blank.
data: String containing your hmtl file, read with BufferedReader for example
More info: WebView.loadDataWithBaseURL(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
XML 布局文件:
Java 代码:
确保准确设置文件路径。
XML Layout File:
Java Code:
Make sure you set file path accurately.
来自官方指南 https://developer.android。 com/develop/ui/views/layout/webapps/load-local-content :
app/src/main/assets/
WebViewAssetLoader
加载资源。在onCreate()
中构造它,如下所示:WebViewClient
包装WebViewAssetLoader
:这基本上将请求 URL 传递给
WebViewAssetLoader
以从资产加载 Web 内容。index.html
可以使用 https 和默认域appassets.androidplatform.net
加载:请注意,使用类似 Web 的 URL 而不是
file 加载本地文件://
是理想的,因为它与同源策略兼容。From the official guide https://developer.android.com/develop/ui/views/layout/webapps/load-local-content :
app/src/main/assets/
WebViewAssetLoader
to load the asset. Construct it in youronCreate()
as follows:WebViewClient
to wrapWebViewAssetLoader
:This basically passes the request URL to
WebViewAssetLoader
to load web content from an asset.assetLoader
from (2) to constructWebViewClient
from (3), and set it in yourWebView
. Yourindex.html
can be loaded by using https and the default domainappassets.androidplatform.net
:Note that loading local files using web-like URLs instead of
file://
is desirable as it is compatible with the Same-Origin policy.接受的答案对我不起作用,这对我有用
The Accepted Answer is not working for me, This is what works for me