Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
570 views
in Technique[技术] by (71.8m points)

image - Sending picture in Android (Socket Programming)

I'm trying to receive a Picture on my Android phone from Server using socket programming.I use this code but in.read() in 4th line always returns -1. How can I fix this problem?

Client Code:

try {
   x = new byte[picLength2];                          
   int current = 0;
   int byteRead = in.read(x, 0, x.length);                          
   current = byteRead;
   do {
      byteRead = in.read(x, current, (x.length - current));
      if (byteRead >0) {
        current += byteRead;
      }
   } while (byteRead > -1);
}catch (IOException e){
   e.printStackTrace();
}

Server Code:

 try {
                                            
     File myFile = new File("C:/onlinegame/null.jpg");
     mybytearray = new byte [(int)myFile.length()];
     FileInputStream fis = new FileInputStream(myFile);
     BufferedInputStream bis = new BufferedInputStream(fis);
     bis.read(mybytearray,0,mybytearray.length);
 } catch (FileNotFoundException e) {
     e.printStackTrace();
 }
 try {
     DataOutputStream out = new DataOutputStream(socket.getOutputStream());
     out.write(mybytearray);
        
  } catch (IOException ex) {
     Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
  }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...