HTML 中“filesDir”中的相对路径不起作用
我正在尝试使用 webview.loadDataWithBaseURL(baseUrl, htmlString)
从 filesDir
加载 html。 HTML 中的相对路径将不起作用,因此它会在没有样式、javascript 和图像的情况下加载。
我提供的 baseUrl
是 file:///data/user/0/com.mydomain.app/files/folder/index.html
当问题得到解决时baseUrl
更改为 file:///android_asset/folder/index.html
。
注意:目前资源存在于 android_asset
和 filesDir
文件夹中;但我计划从 android_asset
文件夹中删除资产。
在应用程序文件结构
main
assets
folder
index.html
utils
style.css
script.js
images
image.jpg
java
res
filesDir
(持久文件)结构
com.mydomain.app
cache
files
folder
index.html
utils
style.css
script.js
images
image.jpg
中,index.html 的内容作为 htmlString
传递。
<head>
<link rel="stylesheet" href="../utils/style.css"/>
</head>
<body>
<img src="../images/image.jpg"/>
<script src="../utils/script.css" />
</body>
I am trying to load html from filesDir
using webview.loadDataWithBaseURL(baseUrl, htmlString)
. The relative paths in the HTML won't work hence it loads without styles, javascript and images.
The baseUrl
that I am providing is file:///data/user/0/com.mydomain.app/files/folder/index.html
The issue gets resolved when the baseUrl
is changed to file:///android_asset/folder/index.html
.
Note : Currently the assets are present in both android_asset
as well as filesDir
folders; but I am planning to remove the assets from android_asset
folder.
In App file structure
main
assets
folder
index.html
utils
style.css
script.js
images
image.jpg
java
res
filesDir
(Persistant Files) structure
com.mydomain.app
cache
files
folder
index.html
utils
style.css
script.js
images
image.jpg
Content of index.html is passed as htmlString
.
<head>
<link rel="stylesheet" href="../utils/style.css"/>
</head>
<body>
<img src="../images/image.jpg"/>
<script src="../utils/script.css" />
</body>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信您需要在
WebSettings
中调用setAllowFileAccess
才能启用对file://
URL 的访问,因为它们在 API 30 中默认被禁用及以上。I believe you need to call
setAllowFileAccess
inWebSettings
to enable access tofile://
URLs, as they're disabled by default in API 30 and above.