1

c# コード

 private MemoryStream Read() {
        NetworkStream clientStream = _client.GetStream();
        MemoryStream messageStream = new MemoryStream();

    var inbuffer = new byte[65535];

    if(clientStream.CanRead) {
        do {
            int bytesRead = clientStream.Read(inbuffer,0,inbuffer.Length);
            messageStream.Write(inbuffer, 0, bytesRead);
            messageStream.Flush();
            Debug.Print(messageStream.Length + " added " + bytesRead);
        } while(clientStream.DataAvailable); // GOES BEYOND HERE, EVEN THOUGH THERE'S MORE DATA
    }

    messageStream.Position = 0;
    return messageStream;
}

アンドロイドコード

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

                if(resultCode == RESULT_OK){                   
                        if(requestCode == ATTACH_FILE_REQUEST){
                                 writeToSocket("<FILE>generic.jpeg");
                                 InputStream input = null;
                                 String filePath = data.getData().getPath().toString();
                                 try {
                                        input = getContentResolver().openInputStream(data.getData());                  
                                        BufferedInputStream bufferedInputStream = new BufferedInputStream(input);
                                        byte[] bytes = new byte[65535];
                                        ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
                                                while((bufferedInputStream.read(bytes)) != -1){
                                                         byteArray.write(bytes, 0, bytes.length);                                
                                                 }
                                                socketNetworkHelper.writeFile(byteArray.toByteArray());
                                } catch (FileNotFoundException e1) {
                                        // TODO Auto-generated catch block
                                        e1.printStackTrace();
                                        Toast.makeText(this, "Error in file selection", Toast.LENGTH_LONG).show();
                                } catch (IOException e2){
                                        e2.printStackTrace();
                                        Toast.makeText(this, "Input/Output Error", Toast.LENGTH_LONG).show();
                                }        
                        }
                }
}

ソケットが接続されている SocketNetworkHelper.java では:

public void writeFile(byte[] buffer) {

                try {
                        socket.getOutputStream().write(buffer);

                } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
        }

問題は、ファイル全体を取得するにはどうすればよいかということです。

4

1 に答える 1

0

ファイルサイズをパラメーターとして送信することもできます

private MemoryStream Read(int filesize) {
            NetworkStream clientStream = _client.GetStream();
            MemoryStream messageStream = new MemoryStream();
            var inbuffer = new byte[filesize];

            if (clientStream.CanRead) {
                int totalBytesRead = 0;
                do {
                    int bytesRead = clientStream.Read(inbuffer, 0, inbuffer.Length);
                    totalBytesRead += bytesRead;
                    messageStream.Write(inbuffer, 0, bytesRead);
                    messageStream.Flush();
                } while (totalBytesRead < filesize);

            }
            messageStream.Position = 0;
            return messageStream;
于 2013-04-22T12:43:34.367 に答える