Android通过socket发送图像,收到空白图像

时间:2014-05-28 09:38:14

标签: java android sockets

我试图在android中使用两个模拟器发送图像,日志文件给我

05-28 13:55:07.349:I / System.out(26763):收到...... 我可以在data \ files \ output.jpg中看到文件资源管理器中创建的图像,但它是空白的,它的大小= 0k

   package com.javacodegeeks.android.androidsocketserver;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.os.StrictMode;
import android.util.Log;

public class Main extends Activity {

    private static ServerSocket serverSocket;
    private static Socket clientSocket;
    private static InputStream inputStream;
    private static FileOutputStream fileOutputStream;
    private static BufferedOutputStream bufferedOutputStream;
    private static int bufferSize = 3000; // bufferSize temporary hardcoded
    private static int bytesRead;
    private static int totalbytesRead = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                .permitAll().build();
        StrictMode.setThreadPolicy(policy);

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

    private void init() throws IOException {
        // TODO Auto-generated method stub
        serverSocket = new ServerSocket(6000); // Server socket

        System.out.println("Server started. Listening to the port 4444");

        clientSocket = serverSocket.accept();

        byte[] data = new byte[bufferSize]; // create byte array to buffer the
                                            // file

        inputStream = clientSocket.getInputStream();

        String filePath = this.getFilesDir().getPath().toString()
                + "/output.jpg";

        fileOutputStream = new FileOutputStream(filePath);
        // fileOutputStream = new FileOutputStream(file);
        bufferedOutputStream = new BufferedOutputStream(fileOutputStream);

        System.out.println("Receiving...");

        // following lines read the input slide file byte by byte

        bytesRead = inputStream.read(data,0,data.length); 
        totalbytesRead = bytesRead; 



        do { 
        bytesRead = 
        inputStream.read(data, totalbytesRead, (data.length-totalbytesRead)); 
        if(bytesRead >= 0) totalbytesRead += bytesRead; 
        } while(bytesRead > -1); 

        bufferedOutputStream.write(data, 0 , totalbytesRead); 
        bufferedOutputStream.flush(); 
        long end = System.currentTimeMillis(); 
        System.out.println(end); 
        bufferedOutputStream.close(); 
        System.out.println("Sever recieved the file");

    }
}

这是客户端

    package com.javacodegeeks.android.androidsocketclient;

/*
 * This is a simple Android mobile client
 * This application send any file to a remort server when the 
 * send button is pressed
 * Author by Lak J Comspace
 */

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class Transfer extends Activity {

    private Socket client;
    private FileInputStream fileInputStream;
    private BufferedInputStream bufferedInputStream;
    private OutputStream outputStream;
    private Button button;
    private TextView text;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_transfer);
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                .permitAll().build();
        StrictMode.setThreadPolicy(policy);

        button = (Button) findViewById(R.id.button1); // reference to the send
                                                        // button
        text = (TextView) findViewById(R.id.textView1); // reference to the text
                                                        // view

        // Button press event listener
        button.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                try {
                    File file = new File("Desktop:\\test.jpg"); // create file
                                                                // instance

                    client = new Socket("10.0.2.2", 5000);

                    byte[] mybytearray = new byte[(int) file.length()]; // create
                                                                        // a
                                                                        // byte
                                                                        // array
                                                                        // to
                                                                        // file

                    fileInputStream = new FileInputStream(file);
                    bufferedInputStream = new BufferedInputStream(
                            fileInputStream);

                    outputStream = client.getOutputStream();
                    int read_count = 0;
                    while ((read_count = bufferedInputStream.read(mybytearray,
                            0, mybytearray.length)) != -1) {
                        outputStream.write(mybytearray, 0, read_count); // Now
                                                                        // writes
                                                                        // the
                                                                        // correct
                                                                        // amount
                                                                        // of
                                                                        // bytes
                        outputStream.flush();

                    }
                    outputStream.flush();
                    bufferedInputStream.close();
                    outputStream.close();
                    client.close();

                    text.setText("File Sent");

                } catch (UnknownHostException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        });

    }
}

1 个答案:

答案 0 :(得分:0)

删除第一个inputStream.read(),因为你已经读取了整个文件内容但是把它扔掉了。第二次没有什么可读的。