WebView.draw(android.graphics.Canvas) won't draw HTML5 canvas to android.graphics.Canvas -
hey i'm trying draw webview bitmap following way:
customwebview webview = (customwebview) findviewbyid(r.id.chart_webview_renderer); string capturepathstring = environment.getexternalstoragedirectory().getpath() + "/temp/ms_" + system.currenttimemillis() + ".png"; bitmap bm = bitmap.createbitmap(webview.getmeasuredwidth(), webview.getmeasuredheight(), bitmap.config.argb_8888); canvas bigcanvas = new canvas(bm); paint paint = new paint(); int iheight = bm.getheight(); bigcanvas.drawbitmap(bm, 0, iheight, paint); webview.draw(bigcanvas); if (bm != null) { try { outputstream fout = null; file file = new file(capturepathstring); fout = new fileoutputstream(file); bm.compress(bitmap.compressformat.png, 50, fout); fout.close(); fout.flush(); bm.recycle(); } catch (exception e) { e.printstacktrace(); } }
this works fine on tablets have available testing here (galaxy tab 2 , 3). results in white bitmap of correct size on sony xperia z , samsung galaxy s2.
the webpage trying draw bitmap contains html5 canvas , when add normal html it, draw html bitmap fine.
the webview set invisible behind views, although have tried making visible , on top of views produced no different results.
i wasn't able solve issue original method used. added following javascript fix html: https://code.google.com/p/todataurl-png-js/ allow use of
canvas.todataurl()
which returns base64 encoded png image. used
webview.addjavascriptinterface
to allow javascript send base64 encoded image java, saved on device.
a rough example of did this:
// after initializing webview: javascriptinterface jsinterface = new javascriptinterface(); webview.getsettings().setjavascriptenabled(true); webview.addjavascriptinterface(jsinterface, "android"); // class used javascript interface saving image file public class javascriptinterface { @javascriptinterface public void canvastoimage(string base64imagedata){ string capturepathstring = environment.getexternalstoragedirectory().getpath() + "/temp/ms_" + system.currenttimemillis() + ".png"; try{ file file = new file(capturepathstring); file.getparentfile().mkdirs(); fileoutputstream fos = new fileoutputstream(file); byte[] decodedstring = android.util.base64.decode(base64imagedata, android.util.base64.default); fos.write(decodedstring); fos.flush(); fos.close(); }catch(exception e){ e.printstacktrace(); } } } // in javascript looks function canvastoimage(){ var dataurl = canvas.todataurl(); window.android.canvastoimage(dataurl.replace("data:image/png;base64,", "")); }
it isn't clean i'd hope be, works on devices here now!
Comments
Post a Comment