HTML 中“filesDir”中的相对路径不起作用

发布于 2025-01-10 01:45:17 字数 1210 浏览 1 评论 0原文

我正在尝试使用 webview.loadDataWithBaseURL(baseUrl, htmlString)filesDir 加载 html。 HTML 中的相对路径将不起作用,因此它会在没有样式、javascript 和图像的情况下加载。

我提供的 baseUrlfile:///data/user/0/com.mydomain.app/files/folder/index.html 当问题得到解决时baseUrl 更改为 file:///android_asset/folder/index.html

注意:目前资源存在于 android_assetfilesDir 文件夹中;但我计划从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伤感在游骋 2025-01-17 01:45:17

我相信您需要在 WebSettings 中调用 setAllowFileAccess 才能启用对 file:// URL 的访问,因为它们在 API 30 中默认被禁用及以上。

webview.getSettings().setAllowFileAccess(true);

I believe you need to call setAllowFileAccess in WebSettings to enable access to file:// URLs, as they're disabled by default in API 30 and above.

webview.getSettings().setAllowFileAccess(true);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文