android - How to send a bitmap to a java socket server? -


i sending image android client java server socket. presently using following code -

client code

try             {                 if (bmp != null & socket!=null)                 {                     bytearrayoutputstream stream = new bytearrayoutputstream();                     bmp.compress(compressformat.jpeg, 100,stream);                     byte[] bytearray = stream.tobytearray();                     inputstream inn = new bytearrayinputstream(bytearray);                     dos.writeint(bytearray.length);                     int len = 0 ;                     byte [] b = new byte [1024];                     while ((len = inn.read(b)) != -1)                     {                         dos.write(b,0,len);                     }                     dos.flush();                     stream.close();                     inn.close();                     result = in.readline();                 }             }             catch (ioexception ioe)             {                 log.d("exception caught", ioe.getmessage());             } 

server code

try                 {                     int length = in.readint();                     system.out.println("got size");                     int bytesread ;                     int len = 0;                     byte[] buffer = new byte[8192];                     bytearrayoutputstream baos = new bytearrayoutputstream();                     while (len<length)                     {                         bytesread = in.read(buffer,0,(int)math.min(buffer.length, length-len));                         len = len + bytesread;                         baos.write(buffer, 0, bytesread);                     }                     byte [] bytearray = new byte [length];                     bytearray = baos.tobytearray();                     file file = new file (filename+(int)(math.random()*500)+".jpg");                     if (!file.exists())                     {                         file.createnewfile();                     }                     fileoutputstream fos = new fileoutputstream (file);                     fos.write(bytearray);                     fos.close();                     out.println("image received");                 }                 catch (ioexception ioe)                 {                     ioe.printstacktrace();                 } 

however have create 2 streams on client side determine , send size of image compressed jpeg format instead of using bmp.compress() directly on socket output stream. wanted know if possible prevent overhead of 2 streams in client code or there other approach have lesser overhead ?

if don't need keep connection open, don't send length , read end of stream. if need keep open, copy loop isn't correct: can overrun @ end of image. should be:

while (len<length) {     bytesread = in.read(buffer, 0, (int)math.min(buffer.length, length-len));     len = len + bytesread;     baos.write(buffer, 0, bytesread); } 

Comments