在Android应用程序中通过TCP发送图像

在Android应用程序中通过TCP发送图像,第1张

概述我正在尝试使用TCP协议连接两个 android应用程序. 客户端有一个imageView,当你按下按钮时,它应该将该图像发送到服务器,并在服务器读取后,它会显示图像. 但我无法在服务器中显示发送的图像. 有人可以帮我一把吗? 这是我的服务器代码,它获得了图像并且显示它是显而易见的 package com.example.simpleserver; import java.io.Data 我正在尝试使用TCP协议连接两个 android应用程序.
客户端有一个imageVIEw,当你按下按钮时,它应该将该图像发送到服务器,并在服务器读取后,它会显示图像.
但我无法在服务器中显示发送的图像.
有人可以帮我一把吗?

这是我的服务器代码,它获得了图像并且显示它是显而易见的

package com.example.simpleserver;    import java.io.DatainputStream;    import java.io.IOException;    import java.io.inputStream;    import java.net.ServerSocket;    import java.net.socket;    import androID.app.Activity;    import androID.graphics.Bitmap;    import androID.graphics.BitmapFactory;    import androID.os.Bundle;    import androID.os.Handler;    import androID.os.Message;    import androID.Widget.ImageVIEw;    import androID.Widget.TextVIEw;     public class SimpleServer extends Activity {       ServerSocket ss = null;       Thread myCommsThread = null;       protected static final int MSG_ID = 0x1337;       public static final int SERVERPORT = 6000;       private Bitmap bitmap;       @OverrIDe      public voID onCreate(Bundle savedInstanceState) {       super.onCreate(savedInstanceState);       setContentVIEw(R.layout.activity_simple_server);       TextVIEw tv = (TextVIEw) findVIEwByID(R.ID.textVIEw01);       tv.setText("nothing from clIEnt yet");       this.myCommsThread = new Thread(new CommsThread());       this.myCommsThread.start();       }       @OverrIDe       protected voID onStop() {       super.onStop();       try {            // make sure you close the socket upon exiting           ss.close();        } catch (IOException e) {           e.printstacktrace();        }       }       Handler myUpdateHandler = new Handler() {        public voID handleMessage(Message msg) {            switch (msg.what) {                case MSG_ID:                     ImageVIEw tv = (ImageVIEw) findVIEwByID(R.ID.imageVIEwServer);                     tv.setimageBitmap(bitmap);                     break;                   default:                       break;                   }             super.handleMessage(msg);         }      };       class CommsThread implements Runnable {        public voID run() {           Socket s = null;            try {            ss = new ServerSocket(SERVERPORT );        } catch (IOException e) {            e.printstacktrace();        }        while (!Thread.currentThread().isInterrupted()) {            Message m = new Message();            m.what = MSG_ID;            try {                if (s == null)                    s = ss.accept();                inputStream in = s.getinputStream();                DatainputStream dis = new DatainputStream(in);                int len = dis.readInt();                byte[] data = new byte[len];                if (len > 0) {                    dis.readFully(data);                }                bitmap = BitmapFactory.decodeByteArray(data,data .length);                myUpdateHandler.sendMessage(m);            } catch (IOException e) {                e.printstacktrace();            }        }    }    }    }

这是我的客户代码,它传达了图像

package com.example.simpleclIEnt;    import java.io.DataOutputStream;    import java.io.IOException;    import java.io.OutputStream;    import java.net.InetAddress;    import java.net.socket;    import java.net.UnkNownHostException;    import java.nio.ByteBuffer;    import androID.app.Activity;    import androID.graphics.Bitmap;    import androID.graphics.drawable.BitmapDrawable;    import androID.os.Bundle;    import androID.vIEw.VIEw;    import androID.Widget.ImageVIEw;    public class SimpleClIEnt extends Activity {        private Socket socket;        private static final int SERVERPORT = 5000;        private static final String SERVER_IP = "10.0.2.2";        @OverrIDe        public voID onCreate(Bundle savedInstanceState) {            super.onCreate(savedInstanceState);            setContentVIEw(R.layout.activity_simple_clIEnt);                    new Thread(new ClIEntThread()).start();        }        public voID onClick(VIEw vIEw) {            try {;                getBytes();            } catch (UnkNownHostException e) {                e.printstacktrace();            } catch (IOException e) {                e.printstacktrace();            } catch (Exception e) {                e.printstacktrace();            }        }        public voID getBytes() throws IOException{            ImageVIEw iv=(ImageVIEw)findVIEwByID(R.ID.imageVIEw1);            //convert the image to bitmap to be send in the intent            Bitmap bmp=((BitmapDrawable)iv.getDrawable()).getBitmap();            int bytes = bmp.getByteCount();            ByteBuffer buffer = ByteBuffer.allocate(bytes); //Create a new buffer            bmp.copyPixelsToBuffer(buffer); //Move the byte data to the buffer            byte[] array = buffer.array();             int start=0;            int len=array.length;            if (len < 0)                throw new IllegalArgumentException("Negative length not allowed");            if (start < 0 || start >= array.length)                throw new indexoutofboundsexception("Out of bounds: " + start);            OutputStream out = socket.getoutputStream();             DataOutputStream dos = new DataOutputStream(out);            dos.writeInt(len);            if (len > 0) {                dos.write(array,start,len);            }        }        class ClIEntThread implements Runnable {            @OverrIDe            public voID run() {                try {                    InetAddress serverAddr = InetAddress.getByname(SERVER_IP);                    socket = new Socket(serverAddr,SERVERPORT);                } catch (UnkNownHostException e1) {                    e1.printstacktrace();                } catch (IOException e1) {                    e1.printstacktrace();                }            }        }    }
解决方法 尝试以下代码将您的图像放在一个字节数组中:

ImageVIEw iv=(ImageVIEw)findVIEwByID(R.ID.imageVIEw);     Bitmap bmp=((BitmapDrawable)iv.getDrawable()).getBitmap();     ByteArrayOutputStream baos = new ByteArrayOutputStream();     bmp.compress(Bitmap.CompressFormat.JPEG,100,baos);     byte array [] = baos.toByteArray();

此外,我必须注意你在主线程上执行getBytes().这仅适用于较旧的AndroID版本.最好把它放在你的线程中.

总结

以上是内存溢出为你收集整理的在Android应用程序中通过TCP发送图像全部内容,希望文章能够帮你解决在Android应用程序中通过TCP发送图像所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1122243.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-29
下一篇2022-05-29

发表评论

登录后才能评论

评论列表(0条)

    保存