I'm trying to send bmp image using socket. I have such code on android:
ByteArrayOutputStream stream = new ByteArrayOutputStream();
MainActivity.bmp.compress(Bitmap.CompressFormat.JPEG, 20,
stream);
byte[] byteArray = stream.toByteArray();
OutputStream os = echoSocket.getOutputStream();
os.write(byteArray,0,byteArray.length);
os.flush();
and on PC:
InputStream in_ = clientSocket.getInputStream();
OutputStream out_ = new FileOutputStream("filename.bmp");
final byte[] buffer = new byte[1024];
int read = -1;
int i = 0;
while ((read = in_.read(buffer)) != -1) {
out_.write(buffer, 0, read);
System.out.println(i);
i++;
}
in_.close();
out_.close();
System.out.println("Done");
It never gets to last line( println("Done") ). It only does when I close android program, it gets to last line and bmp opens succesfully. problem is in_.read waits after android has finished transmitting, and i can't make it work.
You never close the socket/OutputStream on the device side so the PC side doesn't know that there is no more data and so it just spins in the while loop reading 0 bytes at a time.
Also, if you're going to use my solution, please mark me as the accepted answer in your previous thread.