Android Socket Image

package com.example.qing.socketsendimage;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;

public class MainActivity extends AppCompatActivity {
    Button sendImage;
    ImageView imageview;
    public void sendTextMsg(DataOutputStream out, String msg) throws IOException {
        byte[] bytes = msg.getBytes();
        long len = bytes.length;
        //先发送长度,在发送内容
        out.writeLong(len);
        out.write(bytes);
    }
    public void sendImgMsg(DataOutputStream out ) throws IOException {
        //发送的图片为图标,就是安卓机器人,将bitmap转为字节数组
        Log.i("sendImgMsg", "len: "+"1");
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
//        imageview.setImageBitmap(bitmap);

        Log.i("sendImgMsg", "len: "+"2");
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG,80,bout);
        //写入字节的长度,再写入图片的字节
        long len = bout.size();
        //这里打印一下发送的长度
        Log.i("sendImgMsg", "len: "+len);
        out.writeLong(len);
        out.write(bout.toByteArray());
    }

    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sendImage=(Button)findViewById(R.id.buttonSend);
        imageview=(ImageView)findViewById(R.id.image1);

        sendImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                imageview.setImageResource(R.mipmap.ic_launcher);
                new Thread() {
                    Socket socket;
                    String host="192.168.91.1";
                    int post=8999;
                    public void run() {
                        try {
                            //建立连接
                            socket = new Socket(host, post);
                            //获取输出流,通过这个流发送消息
                            DataOutputStream out = new DataOutputStream(socket.getOutputStream());
                            //发送文字消息
//                            sendTextMsg(out,"来自手机客户端的消息");
                            sendImgMsg(out);
                            out.close();
                            socket.close();

                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                    //启动线程
                }.start();



            }
        });

    }

}
package listspackage;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.SQLException;
import java.util.Base64;
public class Service {
    private static Socket socket;
  //定义端口号
    private static final int POST = 8999;

    public static void main(String[] args) {
        try {
          //接收消息
            getMsg();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void getMsg() throws IOException {
        System.out.println("开始连接");
        ServerSocket serverSocket = new ServerSocket(POST);
        Socket socket = serverSocket.accept();
      //获取输入流,通过这个流来读取消息
        DataInputStream input = new DataInputStream(socket.getInputStream());
        
        //接收文字消息
//        getTextMsg(input);
        getImgMsg(input);
        input.close();
        socket.close();
        serverSocket.close();
        System.out.println("通讯结束");
    }
    
    public static void getImgMsg(DataInputStream input) throws IOException {
  	  //同样是先读长度
    	System.out.println("ok");
  	    long len = input.readLong();
  	    System.out.println("len = " + len);
  	    byte[] bytes = new byte[(int) len];
  	  //然后在读这个长度的字节到字节数组
  	    input.readFully(bytes);
  	  //将独到的内容保存为文件到本地
  	    File file = new File("C:\\Users\\Administrator\\Desktop\\" + len + ".png");
  	    FileOutputStream fileOutputStream = new FileOutputStream(file);
  	    fileOutputStream.write(bytes);
  	    System.out.println("ok");
  	}

    public static String getTextMsg(DataInputStream input) throws IOException {
      //一样先读长度,再根据长度读消息
        long len = input.readLong();
        System.out.println("len = " + len);
        byte[] bytes = new byte[(int) len];
        input.read(bytes);
        String msg = new String(bytes);
        System.out.println("msd = " + msg);
        return msg;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值