android - I have saved a drawing in a bitmap in my Database, how can I redraw it? -
in activity can make signature drawing , save in sqlite database "savetodatabase" button. when re-open activity want load drawing database, can't figure out how to! can help? decoded bitmap @ "adding signature database", need next?
here code:
public class werkbondetailsactivity extends activity{ public werkbon chosenworkorder; button mclear, mgetsign, savetodatabase; linearlayout mcontent; signature msignature; public static string tempdir; public int count = 1; public string current = null; private bitmap mbitmap; view mview; database db = new database(this); int chosenworkorderposition = 0; paint paintpiefill; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); requestwindowfeature(window.feature_no_title); setcontentview(r.layout.activity_werkbondetails); tempdir = environment.getexternalstoragedirectory() + "/" + getresources().getstring(r.string.external_dir) + "/"; preparedirectory(); current = count + ".png"; mcontent = (linearlayout) findviewbyid(r.id.linearlayout); msignature = new signature(this, null); msignature.setbackgroundcolor(color.white); mcontent.addview(msignature, layoutparams.fill_parent, layoutparams.fill_parent); mclear = (button)findviewbyid(r.id.btn_handtekeningwissen); mgetsign = (button)findviewbyid(r.id.getsign); savetodatabase = (button)findviewbyid(r.id.save); mview = mcontent; bundle extras = getintent().getextras(); if (extras != null) { chosenworkorderposition = (int)extras.getint("chosenworkorderposition"); } chosenworkorder = db.getwerkbon(chosenworkorderposition + 1); mclear.setonclicklistener(new onclicklistener() { public void onclick(view v) { log.v("log_tag", "panel cleared"); msignature.clear(); } }); mgetsign.setonclicklistener(new onclicklistener() { public void onclick(view v) { log.v("log_tag", "panel saved"); mview.setdrawingcacheenabled(true); msignature.save(mview); } }); savetodatabase.setonclicklistener(new onclicklistener() { public void onclick(view v) { mbitmap = bitmap.createbitmap (mcontent.getwidth(), mcontent.getheight(), bitmap.config.rgb_565);; canvas canvas = new canvas(mbitmap); v.draw(canvas); bytearrayoutputstream stream = new bytearrayoutputstream(); mbitmap.compress(bitmap.compressformat.png, 100, stream); byte[] bitmapdata = stream.tobytearray(); chosenworkorder.sethandtekening(bitmapdata); db.updatewerkbon(chosenworkorder); } }); //adding signature database if(chosenworkorder.gethandtekening() != null){ bytearrayinputstream stream = new bytearrayinputstream(chosenworkorder.gethandtekening()); bitmap bm = bitmapfactory.decodestream(stream); } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } private boolean preparedirectory() { try { if (makedirs()) { return true; } else { return false; } } catch (exception e) { e.printstacktrace(); toast.maketext(this, "could not initiate file system.. sdcard mounted properly?", 1000).show(); return false; } } private boolean makedirs() { file tempdir = new file(tempdir); if (!tempdir.exists()) tempdir.mkdirs(); if (tempdir.isdirectory()) { file[] files = tempdir.listfiles(); (file file : files) { if (!file.delete()) { system.out.println("failed delete " + file); } } } return (tempdir.isdirectory()); } public class signature extends view { private static final float stroke_width = 5f; private static final float half_stroke_width = stroke_width / 2; private paint paint = new paint(); private path path = new path(); private float lasttouchx; private float lasttouchy; private final rectf dirtyrect = new rectf(); public signature(context context, attributeset attrs) { super(context, attrs); paint.setantialias(true); paint.setcolor(color.blue); paint.setstyle(paint.style.stroke); paint.setstrokejoin(paint.join.round); paint.setstrokewidth(stroke_width); } public void save(view v) { log.v("log_tag", "width: " + v.getwidth()); log.v("log_tag", "height: " + v.getheight()); if(mbitmap == null) { mbitmap = bitmap.createbitmap (mcontent.getwidth(), mcontent.getheight(), bitmap.config.rgb_565);; } canvas canvas = new canvas(mbitmap); string ftosave = tempdir + current; file file = new file(ftosave); try { fileoutputstream mfileoutstream = new fileoutputstream(file); v.draw(canvas); mbitmap.compress(bitmap.compressformat.png, 90, mfileoutstream); mfileoutstream.flush(); mfileoutstream.close(); string url = images.media.insertimage(getcontentresolver(), mbitmap, "title", null); log.v("log_tag","url" + url); } catch(exception e) { log.v("log_tag", e.tostring()); } } public void clear() { path.reset(); invalidate(); } @override protected void ondraw(canvas canvas) { canvas.drawpath(path, paint); } @override public boolean ontouchevent(motionevent event) { float eventx = event.getx(); float eventy = event.gety(); switch (event.getaction()) { case motionevent.action_down: path.moveto(eventx, eventy); lasttouchx = eventx; lasttouchy = eventy; return true; case motionevent.action_move: case motionevent.action_up: resetdirtyrect(eventx, eventy); int historysize = event.gethistorysize(); (int = 0; < historysize; i++) { float historicalx = event.gethistoricalx(i); float historicaly = event.gethistoricaly(i); expanddirtyrect(historicalx, historicaly); path.lineto(historicalx, historicaly); } path.lineto(eventx, eventy); break; default: debug("ignored touch event: " + event.tostring()); return false; } invalidate((int) (dirtyrect.left - half_stroke_width), (int) (dirtyrect.top - half_stroke_width), (int) (dirtyrect.right + half_stroke_width), (int) (dirtyrect.bottom + half_stroke_width)); lasttouchx = eventx; lasttouchy = eventy; return true; } private void debug(string string) { } private void expanddirtyrect(float historicalx, float historicaly) { if (historicalx < dirtyrect.left) { dirtyrect.left = historicalx; } else if (historicalx > dirtyrect.right) { dirtyrect.right = historicalx; } if (historicaly < dirtyrect.top) { dirtyrect.top = historicaly; } else if (historicaly > dirtyrect.bottom) { dirtyrect.bottom = historicaly; } } private void resetdirtyrect(float eventx, float eventy) { dirtyrect.left = math.min(lasttouchx, eventx); dirtyrect.right = math.max(lasttouchx, eventx); dirtyrect.top = math.min(lasttouchy, eventy); dirtyrect.bottom = math.max(lasttouchy, eventy); } }//signature }
retrieve byte array database , can decode method:
private static bitmap deserialize(byte[] data) throws ioexception, classnotfoundexception { bytearrayinputstream in = new bytearrayinputstream(data); objectinputstream = new objectinputstream(in); return (bitmap) is.readobject(); }
Comments
Post a Comment