首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android通过socket发送图片,接收到空白图片

问Android通过socket发送图片,接收到空白图片
EN

Stack Overflow用户
提问于 2014-05-28 17:38:14
回答 1查看 874关注 0票数 0

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

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

代码语言:javascript
复制
   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");

    }
}

这是客户端

代码语言:javascript
复制
    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();
                }

            }
        });

    }
}
EN

回答 1

Stack Overflow用户

发布于 2014-05-29 01:05:54

删除第一个inputStream.read(),因为您已经读取了整个文件内容,但将其丢弃。第二次没有更多的东西可读了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23907987

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档