title: Android: Bitmap to Byte Array and back: SkImageDecoder::Factory returned null
body: <p>The goal is to convert a <code>Bitmap</code> to a <code>byte []</code>, pass it between activities in a <code>Bundle</code> of data, then reconvert it back to a <code>Bitmap</code> at a later stage for display in an <code>Imageview</code>.</p>
  
  <p>The issue is that whenever I try this, I just get a null bitmap and the non-descriptive, unhelpful log output:</p>
  
  <p><code>12-07 17:01:33.282: D/skia(2971): --- SkImageDecoder::Factory returned null</code></p>
  
  <p>I have looked at the following solutions:</p>
  
  <p><a href="https://stackoverflow.com/questions/10191871/converting-bitmap-to-bytearray-android">Solution supplies the bitmap to byte[] code used</a></p>
  
  <p><a href="https://stackoverflow.com/questions/10770263/decodebytearray-and-copypixelstobuffer-not-working-skimagedecoderfactory-retu">Highlighted that copyPixelsToBuffer() is essential over .compress</a> </p>
  
  <p>(Especially seeing as it is not necessary in this case).</p>
  
  <p>I have run up the following test case which definitely narrows down the problem to the converting and restoring code. Based on my debugging, there is correct decoding, the byte array is the correct size and full, Bitmap configs are forced to be the same, <code>decodeByteArray</code> is just failing:</p>
  
  <pre><code>package com.example.debug;
  
  import java.nio.ByteBuffer;
  
  import android.os.Bundle;
  import android.app.Activity;
  import android.graphics.Bitmap;
  import android.graphics.Bitmap.Config;
  import android.graphics.BitmapFactory;
  import android.util.Log;
  import android.view.Menu;
  import android.widget.ImageView;
  import android.widget.RelativeLayout;
  
  public class MainActivity extends Activity {
      RelativeLayout rl = null;
      RelativeLayout.LayoutParams rlp = null;
  
      ImageView ivBef = null;
      ImageView ivAft = null;
  
      Bitmap bmBef = null;
      Bitmap bmAft = null;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          // TEST
          BitmapFactory.Options bmo = new BitmapFactory.Options();
          bmo.inPreferredConfig = Config.ARGB_8888;
  
          bmBef = BitmapFactory.decodeFile("/mnt/sdcard/Debug/001.png", bmo);
          byte[] b = bitmapToByteArray(bmBef);
          bmAft = BitmapFactory.decodeByteArray(b, 0, b.length, bmo);
  
          LinearLayout ll = new LinearLayout(this);
  
          ivBef = new ImageView(this);
          ivBef.setImageBitmap(bmBef);
  
          ivAft = new ImageView(this);
          ivAft.setImageBitmap(bmAft);
  
          ll.addView(ivBef);
          ll.addView(ivAft);
  
          setContentView(ll);
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.activity_main, menu);
          return true;
      }
  
      public static byte[] bitmapToByteArray(Bitmap bm) {
          // Create the buffer with the correct size
          int iBytes = bm.getWidth() * bm.getHeight() * 4;
          ByteBuffer buffer = ByteBuffer.allocate(iBytes);
  
          // Log.e("DBG", buffer.remaining()+""); -- Returns a correct number based on dimensions
          // Copy to buffer and then into byte array
          bm.copyPixelsToBuffer(buffer);
          // Log.e("DBG", buffer.remaining()+""); -- Returns 0
          return buffer.array();
      }
  
  }
  </code></pre>
  
  <p>The before <code>Imageview</code> correctly displays the image, the after <code>ImageView</code> shows nothing (as you would expect with a null bitmap</p>