Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top