如何将可绘制文件夹中的图像放在HTML文件中

Ald*_*ndy 10 android

我的/res/raw/test.html中有一个HTML文件

并使用以下代码在Web视图中显示它

WebView wbview = (WebView)findViewById(R.id.webView1);
InputStream fin;
    try
    {
        fin = getResources().openRawResource(R.raw.manual);
        byte[] buffer = new byte[fin.available()];
        fin.read(buffer);
        fin.close();
        wbview.loadData(new String(buffer), "text/html", "UTF-8");
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
Run Code Online (Sandbox Code Playgroud)

这是我的HTML文件的代码

<html>
    <p>
    Lorem ipsum dolor sit amet,
    </p>
  <table>
   <tr>
         <td>
       <img src=\\"file:///android_asset/test_image2.jpg\"/>    
           <img src="test_image2.jpg" width="50px" alt="Hi">

       <img src=\"res/drawable/test_image.png"/>
       <img src="file:///android_res/drawable/test_image.png" />
       <img src=\"file:///android_res/drawable/test_image.png"\ />
         </td>
   </tr>
</table>
</html>
Run Code Online (Sandbox Code Playgroud)

我想在我的html文件中显示我的资源文件夹中的图像...我尝试了所有可能性仍然无法正常工作.它只显示一个HTML文本,但对于一个图像,我不知道如何显示它

请帮我

Her*_*rry 16

您可以从android资源文件夹加载您的html页面,如下面的代码.

WebView webView = new WebView(this);
webView.loadUrl("file:///android_asset/manual.html");
setContentView(webView);
Run Code Online (Sandbox Code Playgroud)

而且你需要在下面制作你的html.

 <html>
  <p>
   Lorem ipsum dolor sit amet,
  </p>
<table>
  <tr>
      <td>
     <img src="file:///android_asset/test_image2.jpg"/>    
      <img src="file:///android_asset/test_image2.jpg" width="50px" alt="Hi">
     <img src="file:///android_res/drawable/test_image.png"/>
     <img src="file:///android_res/drawable/test_image.png" />
     <img src="file:///android_res/drawable/test_image.png" />
        </td>
      </tr>
    </table>
  </html>
Run Code Online (Sandbox Code Playgroud)

  • +1精确解决方案.建议:将图像放在资产文件夹中,无需编写file:/// android_asset路径,只需使用RelativePath引用它即可.(点) (4认同)
  • @Herry:这适用于您自己的应用程序中的图像。但是您知道我们如何引用捆绑图像,例如`@android:drawable/ic_menu_info_details.png`吗? (2认同)
  • 记得在 proguard 配置文件中添加一条规则,让它也能在生产 apk 上工作: -keepclassmembers class **.R$* { public static &lt;fields&gt;; -保持类**.R$* 见[这里](http://stackoverflow.com/questions/6280188/prevent-proguard-to-remove-specific-drawables?answertab=votes#tab-top) (2认同)