BitmapFactory.decodeByteArray returns null

997 views
Skip to first unread message

Alex K.

unread,
Dec 11, 2012, 4:07:51 AM12/11/12
to robol...@googlegroups.com
Hi all,

I am trying to use Robolectric to test my class which uses SQLiteDatabase to store Bitmaps. However, I cannot make even the simplest code to work. Here is the stripped down test case which fails:

@Test
public void bitmapDecode() {
   Bitmap b = Bitmap.createBitmap(10, 10, Bitmap.Config.RGB_565);

   ByteArrayOutputStream stream = new ByteArrayOutputStream(256);
   b.compress(CompressFormat.PNG, 100, stream);

   byte[] compressedBitmap = stream.toByteArray();

   Bitmap immutableBitmap = BitmapFactory.decodeByteArray(compressedBitmap, 0, compressedBitmap.length);

   assertThat(immutableBitmap, notNullValue());
}


I have @RunWith(RobolectricTestRunner.class) and other tests involving Android classes pass (including example from the Eclipse quick start) so I know Robolectric is configured correctly.

What am I doing wrong?

Thank you,
Alex

Alex K.

unread,
Dec 12, 2012, 3:13:54 PM12/12/12
to robol...@googlegroups.com
Could somebody paste this code in their projects and see if it works for you, please?
Knowing if it is my setup problem or Robolectric issue would help me too!

Thank you,
Alex

Christopher Perry

unread,
Dec 20, 2012, 4:58:51 PM12/20/12
to robol...@googlegroups.com
I'd start by taking a look at Robolectric's implementations of Bitmap, and BitmapFactory.

Alexander Kamotsky

unread,
Jan 6, 2013, 2:19:05 AM1/6/13
to robol...@googlegroups.com
Ah, of course! ShadowBitmap.compress() does not really compress anything. In fact, it writes a text in the output stream https://github.com/pivotal/robolectric/blob/master/src/main/java/com/xtremelabs/robolectric/shadows/ShadowBitmap.java#L28

Similarly, ShadowBitmapFactory creates a new empty bitmap. =\
Reply all
Reply to author
Forward
0 new messages