我想知道如何在 webview 中显示 HTML 页面并引用相对/本地图像。目标是让 html 页面和所有链接的图像包含在 android 应用程序包本身中。
我需要在哪里放置资产(资产目录?)以及如何引用它们以便将它们加载到 webview 中?
谢谢!斯文
您可以将所有 html 和图像放入 assets 目录,例如:
assets\html\
index.html
image1.png
image2.jpg
对 html 文件中图像的所有引用都应采用 file:// url 的形式
<html>
<body>
<img src="file:///android_asset/html/image1.png">
<img src="file:///android_asset/html/image2.jpg">
</body>
</html>
毕竟,只需像往常一样将此 HTML 加载到 WebView:
webView.loadUrl("file:///android_asset/html/index.html");