逻辑:将bitmap转化为Base64,通过调用HTML中的JS,显示到HTML中
(1)android代码
- public String bitmaptoString(Bitmap bitmap) {
- // 将Bitmap转换成Base64字符串
- StringBuffer string = new StringBuffer();
- ByteArrayOutputStream bStream = new ByteArrayOutputStream();
- try {
- bitmap.compress(CompressFormat.PNG, 100, bStream);
- bStream.flush();
- bStream.close();
- byte[] bytes = bStream.toByteArray();
- string.append(Base64.encodeToString(bytes, Base64.NO_WRAP));
- } catch (IOException e) {
- e.printStackTrace();
- }
- System.out.println("string.."+string.length());
- return string.toString();
- }
Bitmap bitmap = ....; //获取bitmap对象
webView.loadUrl("javascript:onSaveCallback(‘data:image/png;base64," + bitmaptoString(bitmap) + "‘)"); 调用JS显示
(2)Html代码
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=UTF-8">
<script type="text/javascript">
function
onSaveCallback(src){
window.hui.showMessage(src);
document.getElementById("test").src
= src;
}
</script>
</head>
<body>
<img id="test" src=""
onclick=""/>
</body>
</html>