title: Android Bitmap to Byte Array per Pixel conversion
body: <p>I'm confused about a decoding bitmap to <code>ByteOutputStream</code></p>
  
  <p>I'm using android Library apw - android pdf writer which uses a method to convert bitmap to byte[] array.</p>
  
  <p>I'm trying to achieve this using toByteArray() method but the output pdf shows a black image instead.</p>
  
  <p>library approach</p>
  
  <pre><code> private byte[] getBitmapData(Bitmap bitmap) {
          byte[] data = null;
          if (bitmap != null) {
              data = new byte[mDataSize];
              int intColor;
              int offset = 0;
              for (int y = 0; y &lt; mHeight; y++) {
                  for (int x = 0; x &lt; mWidth; x++) {
                      intColor = bitmap.getPixel(x, y);
                      data[offset++] = (byte) ((intColor &gt;&gt; 16) &amp; 0xFF);
                      data[offset++] = (byte) ((intColor &gt;&gt; 8) &amp; 0xFF);
                      data[offset++] = (byte) ((intColor &gt;&gt; 0) &amp; 0xFF);
                  }
              }
          }
          return data;
      }
  </code></pre>
  
  <p>My new approach</p>
  
  <pre><code>private byte[] getBitmapData(Bitmap bitmap) {
      if (bitmap != null) {
          ByteArrayOutputStream stream = new ByteArrayOutputStream();
          bitmap.compress(Bitmap.CompressFormat.JPEG, 80, stream);
          bitmap.recycle();
          return stream.toByteArray();
      }
      return null;
  }
  </code></pre>
  
  <p>I'm inexperienced in bitwise logic and conversation.
  I couldn't exactly understand the library method.</p>
  
  <p>Is there something am i missing here?
  A little help will be appreciated</p>