Logic: Convert bitmap to Base64 by invoking JS in HTML and displaying it in HTML
(1) Android code
- Public String bitmaptostring (Bitmap Bitmap) {
- //Convert bitmap to Base64 string
- StringBuffer string = new stringbuffer ();
- Bytearrayoutputstream Bstream = new bytearrayoutputstream ();
- Try {
- Bitmap.compress (compressformat.png, 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 = ....; Get Bitmap Object
Webview.loadurl ("Javascript:onsavecallback (' data:image/png;base64," + bitmaptostring (bitmap) + "')"); Invoke JS Display
(2) HTML code
<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>
<body>
</body>