Sending picture in Android (Socket Programming)

自闭症网瘾萝莉.ら 提交于 2019-11-27 04:55:00

问题


I'm trying to Receive Picture on my Android phone From Server using socket programming.

I use this code but in.read() in fourth line always return -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);
  }

来源:https://stackoverflow.com/questions/32991080/sending-picture-in-android-socket-programming

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!