0

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

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

在这一点上卡在 Android 代码上。位图位图 = 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);
4

0 回答 0