Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/208.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.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
Android:如何通过套接字正确发送图片_Android_Sockets - Fatal编程技术网

Android:如何通过套接字正确发送图片

Android:如何通过套接字正确发送图片,android,sockets,Android,Sockets,我试图修改我的android代码,它最初只能发送字符串。任务是让它可以传送图片。我知道有很多不同的方法来实现这一点。但我试过了,真的不知道为什么我的照片不能发送,尽管连接并没有问题。 有人能告诉我吗?我会非常感激的。 我只是一个初学者,请忘记我糟糕的编程技巧 下面是服务器的主要部分 private Runnable socket_server = new Runnable(){ public void run(){ handler.post(new Runnable()

我试图修改我的android代码,它最初只能发送字符串。任务是让它可以传送图片。我知道有很多不同的方法来实现这一点。但我试过了,真的不知道为什么我的照片不能发送,尽管连接并没有问题。 有人能告诉我吗?我会非常感激的。 我只是一个初学者,请忘记我糟糕的编程技巧

下面是服务器的主要部分

private Runnable socket_server = new Runnable(){
    public void run(){
        handler.post(new Runnable() {
            public void run() {
                test.setText("Listening...." + getMyIp());

            }
        });
        try{ 
            serverSocket = new ServerSocket(1234);

            while (true) {
                Socket client = serverSocket.accept();

                handler.post(new Runnable() {
                    public void run() {
                        test.setText("Connected.");
                    }
                });
                try {
                     File myFile = new File ("/sdcard/DCIM/img2.jpg");
                      byte [] mybytearray  = new byte [(int)myFile.length()];
                      FileInputStream fis = new FileInputStream(myFile);
                      BufferedInputStream bis = new BufferedInputStream(fis);
                      bis.read(mybytearray,0,mybytearray.length);
                      OutputStream os = client.getOutputStream();
                      os.write(mybytearray,0,mybytearray.length);
                      os.flush();
                      client.close();
                    test.setText("Received.");

                } catch (Exception e) {
                    handler.post(new Runnable() {
                        public void run() {
                            test.setText("Sending erro");
                        }
                    });
                }
            }
        }catch(IOException e){
            handler.post(new Runnable() {
                public void run() {
                    test.setText("Fail to buitl the socket");
                }
            });
        }
    }
};
这是客户端的一部分

new Thread() 
            {
                @Override 
                public void run() { 
                    // TODO Auto-generated method stub 
                    InetAddress serverAddr = null;
                    SocketAddress sc_add = null;
                    Socket socket = null;

                    try 
                    { 
                        serverAddr = InetAddress.getByName("192.168.1.105");
                        sc_add= new InetSocketAddress(serverAddr,1234);
                        socket = new Socket();
                        socket.connect(sc_add,2000);

                        File myFile = new File("/sdcard/DCIM/img2.jpg");
                        InputStream fis = new FileInputStream("/sdcard/DCIM/img2.jpg");
                        OutputStream outputStream = socket.getOutputStream();

                        byte [] buffer = new byte[(int)myFile.length()];

                        int temp = 0 ;  
                        while((temp = fis.read(buffer)) != -1){  
                            outputStream.write(buffer, 0, temp);  
                        }  
                        outputStream.flush();
                        socket.close();


                } catch (UnknownHostException e) {

                    //TextView01.setText("InetAddress fail");

                } catch (SocketException e) {

                    //TextView01.setText("fail to develop socket");

                } catch(IOException e) {

                    //TextView01.setText("fail to sending");

                }
                } 

            }.start();
这是你的问题:

byte [] mybytearray  = new byte [(int)myFile.length()];
// ...
bis.read(mybytearray,0,mybytearray.length);
OutputStream os = client.getOutputStream();
os.write(mybytearray,0,mybytearray.length); // <- This one.


此外,缓冲区不应该像整个文件那么大,因为分块传输文件更有效。有关详细的解释,请参阅这个老问题:

非常感谢您给出了明确的答案。但是我尝试了你展示的方式,似乎我的程序还有一些问题。。。
byte buffer[] = new byte[1024];
int read_count = 0;
while ((read_count = input.read(buffer, 0, buffer.length)) != -1) {
    output.write(buffer, 0, read_count); // Now writes the correct amount of bytes
}