Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/217.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java tcp服务器到android设备_Java_Android_Image_Sockets_Tcp - Fatal编程技术网

java tcp服务器到android设备

java tcp服务器到android设备,java,android,image,sockets,tcp,Java,Android,Image,Sockets,Tcp,我正在尝试制作一个java服务器,将存储在数据库中的图像发送到android设备 目前,我正在尝试使用存储在硬盘上的简单映像,以便服务器发送 我的问题是,我可以通过TCP套接字从java服务器将图像作为字节数组发送到android。是的,可以,使用TCP套接字 以字节的形式读取文件并以字节的形式发送,不要尝试将其作为BuffereImage加载 然后,在接收端,使用允许您从字节数组加载图像的函数。当然可以,但您需要为此发明自己的协议。您可能会发现使用HTTP更多,或者在部署应用程序的情况下设置T

我正在尝试制作一个java服务器,将存储在数据库中的图像发送到android设备

目前,我正在尝试使用存储在硬盘上的简单映像,以便服务器发送


我的问题是,我可以通过TCP套接字从java服务器将图像作为字节数组发送到android。

是的,可以,使用TCP套接字

以字节的形式读取文件并以字节的形式发送,不要尝试将其作为BuffereImage加载


然后,在接收端,使用允许您从字节数组加载图像的函数。

当然可以,但您需要为此发明自己的协议。您可能会发现使用HTTP更多,或者在部署应用程序的情况下设置Tomcat之类的web服务器,或者使用Jetty之类的嵌入式设备,我尝试将服务器端的图像作为缓冲图像读取,然后将其转换为字节数组并发送。我不知道如何在android客户端接收它并在imageview上显示它。这是完整的代码吗,因为它给了我很多错误错误:mybytearray在服务器代码中声明了两次。有一个无与伦比的*/。
The fastest and easiest approach for sending image is as follows...
Try this    
 // Server Side code for image sending

      ServerSocket servsock = new ServerSocket(13250);   
      System.out.println("Main Waiting...");
      Socket sock = servsock.accept();
      System.out.println("Accepted connection : " + sock);

      File myFile = new File ("\sdcard\ab.jpg");
      byte [] mybytearray  = new byte [(int)myFile.length()];
      FileInputStream fis = new FileInputStream(myFile);
      BufferedInputStream bis = new BufferedInputStream(fis);
      bis.read(mybytearray,0,mybytearray.length);*/

      byte [] mybytearray  =new byte[count];
      System.arraycopy(Copy, 0, mybytearray, 0, count);
      OutputStream os = sock.getOutputStream();
      System.out.println("Sending...");
      os.write(mybytearray,0,mybytearray.length);
      os.flush();
      os.close();
      sock.close();
      servsock.close();

//Client side code for image reception

try
{ 
    int filesize=6022386; // filesize temporary hardcoded
    long start = System.currentTimeMillis();
    int bytesRead;
    int current = 0;
    // localhost for testing
    ServerSocket servsocket = new ServerSocket(13267);
    System.out.println("Thread Waiting...");
    Socket socket = servsocket.accept();
    System.out.println("Accepted connection : " + socket);
    System.out.println("Connecting...");
    File f=new File("\sdcard\ab.jpg");
    f.createNewFile();
    // receive file
    byte [] mybytearray  = new byte [filesize];
    InputStream is = socket.getInputStream();
    FileOutputStream fos = new FileOutputStream(f);
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    bytesRead = is.read(mybytearray,0,mybytearray.length);
    current = bytesRead;
   do {
       bytesRead =
       is.read(mybytearray, current, (mybytearray.length-current));
       if(bytesRead >= 0) current += bytesRead;
    } while(bytesRead > -1);
    count=current;
    Copy=mybytearray.clone();
    bos.write(mybytearray, 0 , current);
    bos.flush();
    long end = System.currentTimeMillis();
    System.out.println(end-start);
    bos.close();
    fos.close();
    socket.close();
    servsocket.close();
}
}   catch(IOException e)
{
    System.out.println("errorr");
}