在webview中显示android资产图像文件

时间:2013-10-30 09:33:19

标签: javascript android css dom

在Android webview中,我有js在边缘的右侧添加一个div(在webview上),现在这在我添加背景颜色的div时工作正常。

当想要在div上添加背景图像时,这似乎不起作用。

这是我的js渲染div

var div = document.createElement('div');
                    div.style.width = "20px";
                    div.style.height = "20px";
                    div.style.position = "absolute";
                    div.style.top = yPos + "px";
                    div.style.right = "5px";
                    div.style.backgroundColor = col;
                    div.style.backgroundImage = "url(file:///android_asset/images/ic_menu_notes_pressed.png)";
document.body.appendChild(div);

背景颜色加载正常,但未显示图像 我的图片位于/assets/images/ic_menu_notes_pressed.png


使用以下解决方案()base64

进行更新

js文件

var img = document.createElement('img');
img.style.width = "50px";
img.alt = "Hello";
img.src = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAABbUlEQVRoQ+1YvUoDQRD+ZoNioSD25tw0Qiz0AXwQQWw0QdRK0FIQQXvRRgSfxgfQ0spKC9s0FrmMwx1JsNC97LrmFibNQW527/u7mbsjJP6jxPFDCUzbQXVAHQhUQCMUKGDwcnUgWMLADaI4wIdr88h7TQwoc+LL5x7p4aXnrPuh4M8JcHdlCzBLIG6D+cgJzNAG3b0+O+v+gwB3s22A7gX8mZCwyRBgQY2O3RHg1yLUohxPkiHA5zB4z47BdCXgZwunkyLQsacC+FJgz4ximgKBQvk3uwfwzTfwBQsWUqZV63ugjM6yBRpj5YcW5P0P+fui3gQOmi30ze0v7a8t59xzYFptlPftOgb85Nu/R+uUgKeE6sBQOI2QRshTAY1QqYD3+4B2IY2QRkifhcoM6CT2nEPpt9Hd1QU0Pjc9+Y+X1e3DVjChCTfwnsQTXidauRKIJm3FjdWBikJFK1MHoklbcWN1oKJQ0cqSd+ALnjXMMUm0ItgAAAAASUVORK5CYII=";

document.body.appendChild(IMG);

1 个答案:

答案 0 :(得分:1)

将您的图片(ic_menu_notes_pressed.png)放在资产文件夹中,然后在网页视图中加载网址

webView.loadDataWithBaseURL("file:///android_asset/", html_string, "text/html", "utf-8", null);

现在您可以使用

 <img src="ic_menu_notes_pressed.png">

  <div style="backgroundImage =ic_menu_notes_pressed.png";> </div>