Click here to Skip to main content
15,896,439 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
send multiple images over socket from java server to android client,trouble that you'll see just last image.
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 .

Server java side :
Java
//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 :
Java
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();
         }
     }  



all the images are sent successfuly , i saw the size of them on toast message and that truth, but the problem that i saw just the last image ! why, i think that should i use syncTask or something like that but i dont know how :(
can someone help pleas ?
Posted
Comments
Richard MacCutchan 20-Apr-15 3:58am    
How are you storing the received images?
Member 11325219 20-Apr-15 11:29am    
i put it in bytes array after that i put it on imageview with decodeByteArray
Maciej Los 20-Apr-15 16:03pm    
Do not repost!
http://www.codeproject.com/Questions/898073/java-How-to-send-a-multiple-images-over-socket?arn=9

1 solution

The Solution of this problem that to put all this code in AsyncTask :)
Thanks for all :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900