将包html +资源加载到webview中

use*_*750 13 android webkit assets local webview

我想知道如何在webview中显示HTML页面,并引用相对/本地图像.目标是让html页面和所有链接的图像包含在android应用程序包本身中.

我需要在哪里放置资产(资产目录?)以及如何引用它们以便加载到webview中?

感谢名单!斯文

小智 28

您可以将所有html和图像放到资源目录中,例如:

assets\html\
  index.html
  image1.png
  image2.jpg
Run Code Online (Sandbox Code Playgroud)

对html文件中图像的所有引用都应该是file:// url的形式

<html>
<body>
  <img src="file:///android_asset/html/image1.png">
  <img src="file:///android_asset/html/image2.jpg">
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

毕竟,像往常一样将此HTML加载到WebView:

   webView.loadUrl("file:///android_asset/html/index.html");
Run Code Online (Sandbox Code Playgroud)

  • 我尝试使用`<img rel="nofollow noreferrer" src ="image1.png">`而不是`<img rel="nofollow noreferrer" src ="file:///android_asset/html/image1.png">`它也可以. (3认同)