Android Byte Array to Bitmap Results Null After Receiving From Socket -
i have created server receives byte array c++ client, client send image uchar array(using opencv) , on android receiving data correctly. server on android store data byte array , need convert byte array bitmap. getting null bitmap after using bitmapfactory.decodebytearray
.
here server code receives data , store in byte array
class imagereciver extends thread { public static byte imagebyte[]; private serversocket serversocket; inputstream in; int imagesize=921600;//expected image size 640x480x3 public imagereciver(int port) throws ioexception{ serversocket = new serversocket(port); } public void run() { socket server = null; server = serversocket.accept(); in = server.getinputstream(); bytearrayoutputstream baos = new bytearrayoutputstream(); byte buffer[] = new byte[1024]; int remainingbytes = imagesize; // while (remainingbytes > 0) { int bytesread = in.read(buffer); if (bytesread < 0) { throw new ioexception("unexpected end of data"); } baos.write(buffer, 0, bytesread); remainingbytes -= bytesread; } in.close(); imagebyte = baos.tobytearray(); baos.close(); server.close(); //here conver byte array bitmap bitmap bmp = bitmapfactory.decodebytearray(imagebyte, 0,imagebyte.length); return; } }
i seems code not correct, try this:
try { url myurl = new url(url); final bufferedinputstream bis = new bufferedinputstream(myurl.openstream(), 1024); final bytearrayoutputstream datastream = new bytearrayoutputstream(); bufferedoutputstream out = new bufferedoutputstream(datastream,1024); copy(bis, out); out.flush(); final byte[] data = datastream.tobytearray(); bitmapfactory.options bfo = new bitmapfactory.options(); bfo.insamplesize = 2; bitmap bm = bitmapfactory.decodebytearray(data, 0, data.length, bfo); bis.close(); //use bitmap... } catch (exception e) { //handle ex }
Comments
Post a Comment