0

クライアントアンドロイド(froyo 2.2)からの接続を受信するまで、Javaのサーバープログラムをブロックする必要があります。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