如何通过套接字发送多个图像 [英] java How to send a multiple images over socket

查看:64
本文介绍了如何通过套接字发送多个图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过套接字将多个图像从java服务器发送到android客户端,麻烦你只看到最后一张图片。

我从java服务器向android发送多个图像的大问题通过套接字和所有图像发送都很好,但接收它们的问题,我只看到最后一个。



服务器java端:

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 :

//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);
    }
}





客户端android端:



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();
         }
     }  







所有图片都是成功发送的,我看到了他们在吐司的信息大小和真相,但我看到的问题只是最后一张图片!为什么,我认为我应该使用syncTask或类似的东西,但我不知道如何:(

有人可以帮忙请求吗?




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 ?

推荐答案

解决这个问题,将所有这些代码放在AsyncTask中:)

感谢所有人:)
The Solution of this problem that to put all this code in AsyncTask :)
Thanks for all :)


这篇关于如何通过套接字发送多个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆