14

I have a serialized class which I want to add a bitmap to, but Bitmap doesn't support serialize.

Instead I thought I'd use a parcel instead, but can't get it to work.

Here's some test code using local variables:

    Parcel parcel;
    Bitmap sourceBitmap;
    Bitmap destinationBitmap;
    parcel = Parcel.obtain();

    sourceBitmap = Bitmap.createBitmap(200, 400, Config.ARGB_8888);

    sourceBitmap.writeToParcel(parcel, 0);

    destinationBitmap = Bitmap.CREATOR.createFromParcel(parcel);

I get the following error on the last line above:

09-06 21:18:20.463: DEBUG/skia(17716): Bitmap_createFromParcel unknown config: 0
09-06 21:18:20.473: DEBUG/AndroidRuntime(17716): Shutting down VM
09-06 21:18:20.483: WARN/dalvikvm(17716): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
09-06 21:18:20.493: ERROR/AndroidRuntime(17716): Uncaught handler: thread main exiting due to uncaught exception
09-06 21:18:20.513: ERROR/AndroidRuntime(17716): java.lang.RuntimeException: Failed to unparcel Bitmap
09-06 21:18:20.513: ERROR/AndroidRuntime(17716):     at android.graphics.Bitmap$1.createFromParcel(Bitmap.java:899)
09-06 21:18:20.513: ERROR/AndroidRuntime(17716):     at android.graphics.Bitmap$1.createFromParcel(Bitmap.java:903)
2
  • PARCELABLE_WRITE_RETURN_VALUE use this in place of '0' at 6th line.
    – Noby
    Commented Sep 7, 2011 at 6:20
  • I'm afraid that didn't seem to make any difference :-( Commented Sep 9, 2011 at 20:03

2 Answers 2

16

you have to reset your parcel:

sourceBitmap.writeToParcel(parcel, 0);
parcel.setDataPosition(0);
destinationBitmap = Bitmap.CREATOR.createFromParcel(parcel);
0
2

Bitmap has already pacelled by android

http://developer.android.com/reference/android/graphics/Bitmap.html#writeToParcel(android.os.Parcel, int)

1
  • 1
    Can you give some more detail? It's not serializable, is it? I'm using API7 Commented Sep 18, 2011 at 13:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.