How can I save an image with text overlaying it to the Gallery in Android Studio? -
we trying make app lets user upload image, write text on it, , save new image text.
our current implementation use imageview hold image, use textviews write on top of it.
what best way save image whole? below our current code.
thanks help! :)
protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_generator); final edittext toptextinput = (edittext)findviewbyid(r.id.edittoptext); final edittext bottomtextinput = (edittext)findviewbyid(r.id.editbottomtext); final textview toptextview = (textview)findviewbyid(r.id.toptext); final textview bottomtextview = (textview)findviewbyid(r.id.bottomtext); imageview = (imageview)findviewbyid(r.id.imageview2); button pickimage = (button) findviewbyid(r.id.button2); pickimage.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { intent photopickerintent = new intent(intent.action_pick); photopickerintent.settype("image/*"); startactivityforresult(photopickerintent, select_photo); } }); toptextinput.addtextchangedlistener(new textwatcher() { @override public void beforetextchanged(charsequence charsequence, int i, int i1, int i2) { } @override public void ontextchanged(charsequence charsequence, int i, int i1, int i2) { toptextview.settext(toptextinput.gettext() + ""); } @override public void aftertextchanged(editable editable) { } }); bottomtextinput.addtextchangedlistener(new textwatcher() { @override public void beforetextchanged(charsequence charsequence, int i, int i1, int i2) { } @override public void ontextchanged(charsequence charsequence, int i, int i1, int i2) { bottomtextview.settext(bottomtextinput.gettext() + ""); } @override public void aftertextchanged(editable editable) { } }); } @override protected void onactivityresult(int requestcode, int resultcode, intent imagereturnedintent) { super.onactivityresult(requestcode, resultcode, imagereturnedintent); switch(requestcode) { case select_photo: if(resultcode == result_ok){ try { final uri imageuri = imagereturnedintent.getdata(); final inputstream imagestream = getcontentresolver().openinputstream(imageuri); final bitmap selectedimage = bitmapfactory.decodestream(imagestream); imageview.setimagebitmap(selectedimage); imageview.setscaletype(imageview.scaletype.fit_xy); } catch (filenotfoundexception e) { e.printstacktrace(); } } } }
use this:
public bitmap takescreenshot() { view rootview = getview(); rootview.setdrawingcacheenabled(true); rootview.builddrawingcache(true); bitmap b1 = bitmap.createbitmap(rootview.getdrawingcache(true)); rootview.setdrawingcacheenabled(false); // clear drawing cache return b1; }
and save bitmap image:
public void savebitmap(bitmap bitmap) { if(bitmap==null){ return; } file imagepath1 = new file(savepath,"screenshot.jpg"); fileoutputstream fos; try { fos = new fileoutputstream(imagepath1); bitmap.compress(bitmap.compressformat.jpeg, 100, fos); fos.flush(); fos.close(); } catch (ioexception e) { log.e("grec", e.getmessage(), e); } }
Comments
Post a Comment