title: Properly load 24-bit bitmap data into a 32-bit Bitmap object
body: <p>I'm trying to load a byte array containing the data of a 24-bit bitmap file into a Bitmap object in Android but I'm having trouble since Bitmap only supports 32-bit and 16-bit formats. Here's what I came up with in order to convert the 24-bit data into 32-bit:</p>
  
  <pre><code>byte[] file = new byte[is.available()];
  is.read(file);
  is.close();
  byte[] paddedFile = new byte[file.length + file.length / 3]; //we will be adding 1 more byte (alpha value) for each RGB triplet
  for (int i = 0; i &lt; file.length / 3; i++) {
      paddedFile[i * 4] = file[i * 3];
      paddedFile[i * 4 + 1] = file[i * 3 + 1];
      paddedFile[i * 4 + 2] = file[i * 3 + 2];
      paddedFile[i * 4 + 3] = -1; //manually added alpha channel
  }
  Bitmap screen = Bitmap.createBitmap(479, 616, Bitmap.Config.ARGB_8888); //exact dimensions of the 24-bit bitmap file
  screen.copyPixelsFromBuffer(ByteBuffer.wrap(paddedFile));
  iv.setImageBitmap(screen);
  </code></pre>
  
  <p>It kind of/almost works. Here's the bitmap I'm working with:</p>
  
  <p><img src="https://i.stack.imgur.com/SUtbv.png" alt="bmp"></p>
  
  <p>And here's what shows up after the code above:</p>
  
  <p><img src="https://i.stack.imgur.com/CNzxs.png" alt="paddedbmp"></p>
  
  <p>Why is it distorted? Any leads on how to fix this problem is greatly appreciated.</p>