从java服务器发送多个图像到Android客户端

时间:2012-12-10 17:13:36

标签: java android sockets

我需要阻止Java中的服务器程序,直到它从客户端android(froyo 2.2)收到连接。当您解锁Java服务器时,向他发送图片(多个),Android客户端将接收并存储在SD卡上。

当客户端尝试获取位图时会出现问题,位图被锁定且程序没有响应。

在Android代码上遇到困难。 Bitmap bitmap = BitmapFactory.decodeStream(in);

服务器代码(Java)

server= new ServerSocket(port);

while(true){
try{
    System.out.println("Wait for connect...");
    conex = server.accept();

    // The number of images to be sent.
    dataOutputStream = new DataOutputStream(conex.getOutputStream());
    dataOutputStream.writeInt(Path().list().length);
    dataOutputStream.flush();

    do{
        // Send the images
        if( j <= Path().list().length){
        image = (BufferedImage) ImageIO.read(new File(Path(),"img"+j+".jpg"));

        if (image != null) {
            try{
               System.out.println("Sending img "+"img"+j+".jpg...");
               OutputStream out = conex.getOutputStream();
               ImageIO.write(image, "JPEG", out);
               System.out.println("Image"+"img"+j+".jpg send correctly");
              out.flush();
              j++;
            } catch (IOException excepcionES ) {
              System.out.println( "Can't write object" );
            }
        } else {
            System.out.println("Can't read image...");
        }
       }

    // Wait until client store the image and cofirm that.
    System.out.println("Wait client confirmation...");
    dataInputStream = new DataInputStream(conex.getInputStream());
    modo = dataInputStream.readUTF();
    System.out.println("Receive from client: "+modo);

    }while(modo.equals("OK"));

} catch (EOFException exceptionES) {
    exceptionES.printStackTrace();
} finally {
    dataOutputStream.close();
    conex.close();
}
}

客户端代码(Android 2.2)

try {
client = new Socket(ip,port);
} catch (UnknownHostException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}

// We get the number of images that will be received.

try {
dataInputStream = new DataInputStream(client.getInputStream());
images = dataInputStream.readInt();
} catch (Exception e) {
e.printStackTrace();
}

do{
try {
    InputStream in = client.getInputStream();


            // Get stuck at this point!
    Bitmap bitmap = BitmapFactory.decodeStream(in);


    if(bitmap==null)
        Log.e("Client", "bitmap is null!");

    File sd = Environment.getExternalStorageDirectory();
    dir = new File(sd + "/Images/");

    File f = new File(dir, "image"+ dir.list().length + ".jpg");

    f.createNewFile();
    OutputStream os = new FileOutputStream(f);
    bitmap.compress(Bitmap.CompressFormat.JPEG, 90, os);
    os.close();

    numberOfImgRec++;
} catch (OptionalDataException e1) {
    e1.printStackTrace();
} catch (IOException e1) {
    e1.printStackTrace();
}

try {
    dataOutputStream= new DataOutputStream(client.getOutputStream());

    if(numberOfImgRec != images)
        dataOutputStream.writeUTF("OK");
    else
        dataOutputStream.writeUTF("END");
} catch (IOException e) {
    e.printStackTrace();

}while(numberOfImgRec != images);

0 个答案:

没有答案