Android : send multiple images over socket from java server to android client,trouble that you'll see just last image

on Sunday, April 19, 2015


My big problem that i send multiple images from java server to android clent over socket and all the images sending fine, but the problem with receive them, i see just the last one .


The java server :



//during the connecting
private void whileConnecting() throws IOException, AWTException, InterruptedException {
int i=0;
while(i<5){
System.out.println("i = " + i);
capture = robot.createScreenCapture(screenRectangle);
baos = new ByteArrayOutputStream();
ImageIO.write(capture, "png", baos);
baos.flush();
System.out.println("Size of baos = " + baos.size());
byte[] buffer = baos.toByteArray();
baos.close();
baos = null;
output.writeObject(buffer);
output.flush();
i++;
Thread.sleep(1000);
}
}


Client android side :



try {
connection = new Socket(getIntent().getStringExtra("ip"), 8080);
input = new ObjectInputStream(connection.getInputStream());
int i = 0;
while (i < 5) {
i++;
buffer = (byte[]) input.readObject();
Toast.makeText(getApplicationContext(), "buffer length = " + buffer.length, Toast.LENGTH_LONG).show();
//screenCapture.setImageBitmap(BitmapFactory.decodeByteArray(buffer, 0, buffer.length));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
connection.close();
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "Error with closing connection", Toast.LENGTH_LONG).show();
}
}


if someone know how can i solve this problem i'll thank him alot :), i think that should i use syncTask or something but i dont know use it :(


0 comments:

Post a Comment