As stated, I am trying to send an Image from a Java Server to an Android Client. It worked using Base64, which only worked with png's for me, but that was too slow for my liking, so i wanted to try it with a jpg.
I used Bytearrays and I know that you have to first send the size, then the actual image, which i did.
What happens now is that the first time i send the image, the correct size is sent and the image is displayed (relatively) correctly. But when the image is too large, (keep in mind, the size that is communicated is still correct), some of the image is cut off. It's just not transmitted yet, but the android client just goes with what it has. The problem is, that the missing bytes are somehow still in the InputStream, so next time i try to download the image, the size is waaaaaayyyyy off, sometimes in the negatives. I am assuming that the missing bytes from the last image are just added in front of the size-Bytes of the second image, which crashes the programm.
I tried sleeping the thread at and for different times, which has an impact, but doesn't fix the problem completely. It would help if anybody knew, how i could safely get rid of the lasting bytes, or even better, make it so that the full image is displayed and the client doesn't just go on with half of the bytes.
This is the method in question of the client:
{
out.println("screenshot");
Log.d("screenshot", "try getting screenshot");
InputStream is = s.getInputStream();
byte[] sizeAr = new byte[8];
is.read(sizeAr);
int size = ByteBuffer.wrap(sizeAr).asIntBuffer().get();
Log.d("screenshot", "size: " + size);
//thread.sleep(600);
byte[] imageAr = new byte[size];
//thread.sleep(600);
is.read(imageAr);
//thread.sleep(600);
final Bitmap decodedBitmap = BitmapFactory.decodeByteArray(imageAr, 0, size);
//...and display of image
}
And here's the server code sniplet
image = ImageIO.read(new File("./res/testimage.jpg"));
OutputStream outputStream = socket.getOutputStream();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ImageIO.write(image, "jpg", byteArrayOutputStream);
byte[] result = byteArrayOutputStream.toByteArray();
//os.writeBytes("size coming."+ "\n");
//os.flush();
byte[] size = ByteBuffer.allocate(8).putInt(result.length).array();
outputStream.write(size);
System.out.println("size: " + result.length);
//thread.sleep(500);
outputStream.write(result);
thread.sleep(500);
//outputStream.flush();
this is roughly how an image would look. (after this, the next time i press screenshot, the app crashes) Many thanks in advance :)