2013年9月10日星期二

Android client pick Image . Jijiji

The server sends a few pictures , the client received and stored in the SD card . Sometimes clients can receive successfully , sometimes unsuccessfully . Solved ! ! ! ! !

look at the code :
server side :
/ / end flag pictures
byte [] end_b = new byte [1024];
for (int bi = 0; bi <1024; bi + +)
end_b [bi] = 0;
end_b [0] = 7;
end_b [1] = 8;
end_b [2] = 9;
end_b [3] = 1;
end_b [4] = 2;
end_b [5] = 3;
for (int num = 0; num <4; num + +)
{
FileInputStream file_in_stream = new FileInputStream (new File (". \ \ Images \ \" + num + ". JPG"); ;
byte [] b = new byte [1024];
while (file_in_stream.read (b, 0,1024)> 0)
{
out_stream_send.write (b, 0,1024);
out_stream_send.flush ();
}
/ / transmit end flag , the client receives the flag after the end of the next picture can be received
out_stream_send.write (end_b, 0,1024);
out_stream_send.flush ();
}
System.out.println (" send pictures is completed, close the socket");
CliSocket.shutdownOutput ();
CliSocket.close ();


android client code :
/ / obtain the phone SD card directory , create a folder yourself
File sd = Environment.getExternalStorageDirectory () ;/ / get / SDCARD /
String path = sd.getPath () + "/ erewen";
File file_dir = new File (path);
if (! file_dir.exists ())
{
/ / directory does not exist to create a directory
file_dir.mkdir ();
}
for (int i = 0; i <4; i + +)
{
FileOutputStream file_out_stream = new FileOutputStream (new File (path + "/" + i + ". JPG"));
byte [] b_image = new byte [1024];
while (in_stream2.read (b_image, 0,1024)> 0)
{
file_out_stream.write (b_image, 0,1024);
/ / When a message is received after the end of the flag out of the while loop , ready for the next one picture storage
if (b_image [0] == 7 && b_image [1] == 8 && b_image [2] == 9 & & b_image [3] == 1 && b_image [4] == 2 && b_image [5] == 3)
break;
}
/ / empty the cache , close the connection , prepare the second picture
file_out_stream.flush ();
file_out_stream.close ();
}
CliSocket2.close ();


each picture is probably more than 50 K. also tried successfully pass 104K , so it should not with the size of the picture . android is a real machine with a server over WIFI and LAN .. I have tried both the server and client upload pictures in the computer . computer to receive the same idea code did not have problems . was on the phone in the android client receives sometimes receives twelve unsuccessful, sometimes successfully . request an expert answer the next . brother in this Thank you .
------ Solution ---------------------------------------- ----
forget this fraction gave me another account it.
I will return text content and transfer pictures to separate two parts. transfer pictures to this part of the server -side first obtain a picture of the size , save it in a 4 byte array to the client , the client receives after convert it to an int type, and then sent from the server to receive picture messages.
before receiving pictures poorly written code , because the client at a time from the number of bytes received SOCKET necessarily 1024 .
while (in_stream2.read (b_image, 0,1024)> 0)
{
file_out_stream.write (b_image, 0,1024);
/ / When a message is received after the end of the flag out of the while loop , ready for the next one picture storage
if (b_image [0] == 7 && b_image [1] == 8 && b_image [2] == 9 && b_image [3] == 1 && b_image [4] == 2 && b_image [5] == 3)
break;
}
so to add an int length = in_stream2.read (b_image, 0,1024); to determine how much the client receives the number of bytes

------ For reference only ---------------------------------- -----
server and client are connected via TCP socket
------ For reference only ------------------ ---------------------
dropping a ? The first not to transfer pictures , ping 100 times to see the results ......
------ For reference only ---------------------------------------
likely stick package now. Using a mobile phone would often stick package issue.
------ For reference only -------------------------------------- -
with the 3rd floor , there may be network congestion problem
------ For reference only ------------------------ ---------------
amount , then what specific methods on the basis of this code change it
------ For reference only --- ------------------------------------
best to verify every piece of data ( a simple check the head and tail, plus a special flag ) , and then determine the correct transmission under a ( communication process is hardly guaranteed accurate ) .
------ For reference only -------------------------------------- -
friendship Bangding @ - @
------ For reference only --------------------------- ------------
mark wait landlord
------ For reference only -------------------- -------------------
problem seems to have found , the server before sending the picture also sent a text to the client. told me to send text content of this after commenting out the code segment can receive the picture , but it is sometimes distorted picture , I might send and receive 1024 bytes are used to send and receive , the picture adds some extra bytes , but how after sending the text content of bytes it can successfully send pictures ?

sending text code :
OutputStream out_stream_send = CliSocket.getOutputStream ();
OutputStreamWriter out_writer_send = new OutputStreamWriter (CliSocket.getOutputStream (), "GB2312");
BufferedWriter buf_writer_send = new BufferedWriter (out_writer_send);
String content = null;
/ / get text from a database stored in the content object.
while (rs_send1.next ())
{
if (title.equals (rs_send1.getString (" sub-sub- heading" ) ) )
{
content = rs_send1.getString (" text " ) ;
System.out.println (" sending content :" + content);
}
}
buf_writer_send.write (content + "\ r \ n \ r \ n");
buf_writer_send.flush ();
System.out.println (" sending a text ! then have to send the picture !"); * /
------ For reference only -------- -------------------------------
nobody ah ............ < br> ------ For reference only ---------------------------------------
to the next source to learn about
------ For reference only ---------------------------- -----------
QQ mailbox 893003449@qq.com

没有评论:

发表评论