0

Somebody knows why the method does not return a bitmap? Instead of bitmap, null is returned from the exception. This method is called in another class (which is in a new thread).

Method code:

class ClientIn {

Socket socket;
byte[] lenBuffer;
ReceiveBuffer buffer;

public  ClientIn(Socket s) {
    socket = s;
    lenBuffer = new byte[4];
}

public Bitmap Receive() {

    try {
        InputStream stream = socket.getInputStream();
        stream.read(lenBuffer,0,lenBuffer.length);

        buffer = new ReceiveBuffer(BitConverter.ToInt32(lenBuffer,0)) ;
        while(buffer.toReceive > 0) {
            stream.read(buffer.Buffer, 0, buffer.Buffer.length);
            if (buffer.Buffer.length <= 0) {
                continue;
            }
            else {

                if (buffer.toReceive < 1024) {
                    buffer.bufStream.write(buffer.Buffer, 0, buffer.toReceive);
                    buffer.toReceive -= buffer.toReceive;
                    Arrays.fill(buffer.Buffer, 0, buffer.Buffer.length, (byte) 0);

                }
                else {
                    buffer.bufStream.write(buffer.Buffer, 0, buffer.Buffer.length);
                    buffer.toReceive -= buffer.Buffer.length;                       
                    if (buffer.toReceive > 0) {
                    Arrays.fill(buffer.Buffer, 0, buffer.Buffer.length, (byte) 0);
                    }
                }
            }
        }
        byte[] test123 = buffer.bufStream.toByteArray();
        Bitmap thtImg2 = BitmapFactory.decodeByteArray(test123, 0, test123.length);
        return  thtImg2; // <--- INSTEAD OF BITMAP, METHOD RETURN NULL
    }
    catch (IOException e) {
        e.printStackTrace();
        return null;
    }
} }

The image data received via the socket as a byte array. Then create a bitmap object by BitmapFactory.decodeByteArray (test123, 0, test123.length);

Or bitmap receives bad data or bad has been decoded. Here is the result:

Bad decoded image

Se-BASS-tian
  • 51
  • 1
  • 7
  • Did you look at the documentation for `BitmapFactory.decodeByteArray` carefully? – Jon Skeet Nov 19 '15 at 11:10
  • 1
    Try debugging and find previous values are correct or not – Jas Nov 19 '15 at 11:13
  • 3
    You problem statement and code say different things. Please clarify whether `null` is being returned from the catch-branch or from `BitmapFactory.decodeByteArray()`. In case of the former, could you add the stracktrace too? – MH. Nov 19 '15 at 11:15
  • You can follow this for displaying Bitmaps: http://developer.android.com/training/displaying-bitmaps/index.html – Dhrumil Shah - dhuma1981 Nov 19 '15 at 11:15
  • Of course in a variable byte [] test123 is stored image in .JPG – Se-BASS-tian Nov 19 '15 at 18:04
  • @MH. I compared values of the array before sending on the server side and afrer received data - the variable test123. I checked the first 10 values and are the same, but the last 10 are different. The code responsible for receiving data is probably wrong :/ – Se-BASS-tian Nov 21 '15 at 00:01

0 Answers0