android - converting text string to image -
i converting text string image in android application. following various posts on so, have written code. doesn't display image. there wrong code?
protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); et = (edittext) findviewbyid(r.id.edittext1); iv = (imageview) findviewbyid(r.id.imageview1); btn = (button) findviewbyid(r.id.button1); btn.setonclicklistener(new onclicklistener(){ @override public void onclick(view v) { // todo auto-generated method stub string text = et.gettext().tostring(); byte[] data = null; try { data = text.getbytes("utf-8"); } catch (unsupportedencodingexception e) { // todo auto-generated catch block e.printstacktrace(); } final string base64 = base64.encodetostring(data, base64.default); bitmap bit = stringtobitmap(base64); iv.setimagebitmap(bit); } }); } public bitmap stringtobitmap(string encodedstring){ try{ byte [] encodebyte=base64.decode(encodedstring,base64.default); bitmap bitmap=bitmapfactory.decodebytearray(encodebyte, 0, encodebyte.length); return bitmap; }catch(exception e){ e.getmessage(); return null; } }
try convert byte[] image:
bitmap bit = bitmapfactory.decodebytearray(data, 0, data.length);
there's no reason why should encode byte array base64 decode back.
although real question is: in edittext? image converted string, or text you're trying display?
if it's image, above work.
if not, wrong approach , sounds need do, foamyguy said in comments, draw text onto imageview's canvas: https://stackoverflow.com/a/10923478/1426565
although, if have text, why not display in textview , customize textview display how want (ie. font size, color, background, etc)? better approach. if you're unsure of how use textview, check out: http://developer.android.com/reference/android/widget/textview.html
Comments
Post a Comment