How to convert canvas to bitmap in android? -
i creating app in android user draw on canvas , save user directory i.e. sdcard.
but problem bitmap save shows black image , mean image never saves drawn on canvas black image saves.
here code
import java.io.file; import java.io.filenotfoundexception; import java.io.fileoutputstream; import android.content.context; import android.graphics.bitmap; import android.graphics.bitmap.compressformat; import android.graphics.bitmap.config; import android.graphics.canvas; import android.graphics.color; import android.graphics.paint; import android.graphics.path; import android.os.environment; import android.view.motionevent; import android.view.view; import android.widget.toast; public class drawwrite extends view { float touchxd = 0, touchyd = 0, touchxu = 0, touchyu = 0, touchxm = 0, touchym = 0; // define touch co-ordinates float x1 = 0, y1 = 0, x2 = 0, y2 = 0; // define drawing path co-ordinates float stroke = 2; // define message structure width int i=0; boolean move = false, moved = false, moveu = false; // define whether // touch has occurred or // not boolean exp = false; paint paint = new paint(); // paint object path mpath = new path(); // define drawing message path context context; public drawwrite(context context) { super(context); this.context=context; } @override public void ondraw(canvas canvas) { super.ondraw(canvas); invalidate(); paint.setantialias(true); if (drawwriteactivity.clearscreen){ mpath.reset(); drawwriteactivity.clearscreen = false; } if(drawwriteactivity.saveimage){ try { getdrawnmessage(canvas); drawwriteactivity.saveimage = false; } catch (filenotfoundexception e) { e.printstacktrace(); } } paint.setcolor(color.parsecolor(drawwriteactivity.colorprovider)); paint.setstyle(paint.style.stroke); paint.setstrokewidth(drawwriteactivity.strokeprovider); if (moved == true) { x1 = touchxd; y1 = touchyd; moved = false; } else if (move == true) { x2 = touchxd; y2 = touchyd; mpath.moveto(x1, y1); mpath.lineto(x2, y2); canvas.drawpath(mpath, paint); x1 = x2; y1 = y2; } } @override public boolean ontouchevent(motionevent event) { switch (event.getaction()) { case motionevent.action_down: touchxd = (float) event.getx(); touchyd = (float) event.gety(); moved = true; break; case motionevent.action_up: touchxu = (float) event.getx(); touchyu = (float) event.gety(); moveu = true; break; case motionevent.action_move: touchxd = (float) event.getx(); touchyd = (float) event.gety(); move = true; break; } return true; } public void getdrawnmessage(canvas canvas) throws filenotfoundexception{ bitmap bitmap; setdrawingcacheenabled(true); toast.maketext(getcontext(), "toasting", toast.length_short).show(); string root = environment.getexternalstoragedirectory().tostring(); file imgdir = new file(root+"/chitbak/"); string imgname; bitmap = bitmap.createbitmap(getwidth(), getheight(), config.argb_8888); imgdir.mkdirs(); imgname = "img"+i+".jpg"; i++; file file = new file(imgdir,imgname); if(file.exists()) file.delete(); fileoutputstream outimg = new fileoutputstream(file); bitmap.compress(compressformat.jpeg, 100, outimg); setdrawingcacheenabled(false); } }
i want clarify when use
bitmap = bitmap.createbitmap(getdrawingcache());
method throws stackoverflow exception.
please friends me because pissed off situation week.
your bitmap black because didn't draw on it.
to have drawing in bitmap must indeed create call :
bitmap = bitmap.createbitmap(getdrawingcache());
then have stackoverflow exception because :
getdrawingcache()
callondraw(canvas)
ondraw(canvas)
callgetdrawnmessage(canvas canvas)
getdrawnmessage(canvas canvas)
callgetdrawingcache()
getdrawingcache()
callondraw(canvas)
- ...
so real problem stackoverflow exception.
solution : don't call getdrawnmessage(canvas canvas)
in ondraw(canvas)
method.
Comments
Post a Comment