java - Android BitmapFactory often returning null on Base64 decoded byte array -
i have java server on pc using infamous robot class capture screenshots, encode them base64 strings , send them android client on socket using printwriter
the android client reads base64 encoded strings, converts them byte arrays , bitmap images display in imageview.
the problem while works expected, bitmapfactory.decodebytearray method return null , hence render nothing imageview.
giving me message in logcat d/skia﹕ --- decoder->decode returned false
i can reproduce successful/unsuccessful decodes changing displayed on pc, can simple highlighting line of onscreen text cause decode starting failing , un-highlighting return succeeding.
when decode has failed have tried both comparing string sent server (is 100% same) , validating base64 encode external tool base64 image converter, shows perfect image.
this happens on both real device running android 4.1 , emulated device on android 5.1
have spent 2 days far poring on , internet in general looking answer haven't found quite fits situation.
why bitmapfactory method failing often? or have stuffed terribly in client code?
android client code
public class mainactivity extends activity { private socket socket; private static final int serverport = 6666; private static final string server_ip = "192.168.2.5"; private imageview imgcontainer; private bitmap bitmapobj; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); imgcontainer = (imageview) findviewbyid(r.id.imgcontainer); new thread(new connectionthread()).start(); } private class connectionthread implements runnable { @override public void run() { try { inetaddress serveraddr = inetaddress.getbyname(server_ip); socket = new socket(serveraddr, serverport); bufferedreader in = new bufferedreader(new inputstreamreader(socket.getinputstream())); // first line garbage string instr=in.readline(); //image receiver loop while((instr = in.readline()) != null){ // log shows string same length 1 sent log.d("imgthread", string.valueof(instr.length())); byte[] img64 = base64.decode(instr, base64.default); //fails here, bitmapfactory decode returns null? not bitmapobj = bitmapfactory.decodebytearray(img64, 0, img64.length); // update imgcontainer imageview mainactivity.this.runonuithread(new runnable() { @override public void run() { imgcontainer.setimagebitmap(bitmapobj); } }); } } catch (unknownhostexception e1) { e1.printstacktrace(); } catch (ioexception e1) { e1.printstacktrace(); } } } }
Comments
Post a Comment