title: Turning byte array into Bitmap
body: <p>I have a server in cpp and a client in Java where I am sending an image from the server to the client in segments.  On the client side I pick up all the segment to form a single array of bytes.</p>
  
  <p>My problem is that I don't seem to be able to successfully create a bitmap out of this byte array, since I get null using <code>BitmapFactory.decodeByteArray()</code></p>
  
  <p>Client code:</p>
  
  <pre><code>byte[] response_bytes;
  private Bitmap receive_image ( final String protocol, final int image_size, final int buffer_size)
  {
      if (image_size &lt;= 0 || buffer_size &lt;= 0)
          return null;
  
      Thread image_receiver = new Thread(new Runnable() {
          @Override
          public void run() {
              ByteBuffer byteBuffer = ByteBuffer.allocate(image_size);
              byte[] buffer = new byte[buffer_size];
              int bytesReadSum = 0;
              try {
                  while (bytesReadSum != image_size) {
                      activeReader.read(buffer);
                      String message = new String(buffer);
                      if (TextUtils.substring(message, 0, len_of_protocol_number).equals(protocol)) {
                          int bytesToRead = Integer.parseInt(TextUtils.substring(message, 
                                  len_of_protocol_number, 
                                  len_of_protocol_number + len_of_data_len));
  
                          byteBuffer.put(Arrays.copyOfRange(buffer, 
                                  len_of_protocol_number + len_of_data_len, 
                                  bytesToRead + len_of_protocol_number + len_of_data_len));
                          bytesReadSum += bytesToRead;
                      } else {
                          response_bytes = null;
                          break;
                      }
                  }
                  if (bytesReadSum == image_size) {
                      byte[] image_bytes = byteBuffer.array();
                      if (image_bytes.length &gt; 0)
                          response_bytes = image_bytes;
                      else
                          response_bytes = null;
                  }
              } catch (IOException e) {
                  response_bytes = null;
              }
          }
      });
      image_receiver.start();
      try {
          image_receiver.join();
      } catch (InterruptedException e) {
          response_bytes = null;
      }
  
      if (response_bytes != null) {    //Here image_bitmap is null
          Bitmap image_bitmap = BitmapFactory.decodeByteArray(response_bytes, 0, response_bytes.length);
          return image_bitmap;
      } else {
          return null;
      }
  </code></pre>
  
  <p>At this point I'd also like to mention that the image bytes reading seems to work fine and I manage to read all the bytes of the image without fail.
  Yet, I'm unable to turn that array of bytes into a bitmap.</p>