简体   繁体   中英

Android Java: Receive Image over Socket

i have a communication App over sockets. The Client sends an Image to the server:

ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
        byte[] byteArray = stream.toByteArray();
        OutputStream os;
        try {
            os = MyClient.socket.getOutputStream();
            os.write(byteArray,0,byteArray.length);
            os.flush();

On the server side i want to receive the Image, but at the moments it just shows many different characters. If the Client just sends a text i receive it with:

BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
String text = input.readLine();

But how can i "decode" the byte[] on the Server Side?

Analogously to how you sent that image. Simply use an InputStream object like this:

InputStream stream = socket.getInputStream();
byte[] data = new byte[MAX_SIZE];
int count = stream.read(data);

Both objects (sending and receiving) are compatible this way, you just have to know the byte array size, it has to be the same on both places.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM