Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ClassCastException when unmarshelling parcelable array

I'm basically trying to save an array of Bitmaps between states.

My fragment's onSaveInstanceState:

public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putParcelableArray(SELECTED_IMAGES_ARRAY_BUNDLE, galleryAdapter.getImageBitmaps());
}

And in it's onCreateView I retrieve the array as so:

savedInstanceState.setClassLoader(Bitmap.class.getClassLoader());
Bitmap[] savedSelectedImages = (Bitmap[]) savedInstanceState.getParcelableArray(SELECTED_IMAGES_ARRAY_BUNDLE);

This WORKS regularly, EXCEPT when the Android OS does some memory management, kills off my process if it's running in the background, and later tries to restore it when I come back to it.

This is the error I get:

AndroidRuntime(4985): Caused by: java.lang.ClassCastException: android.os.Parcelable[] cannot be cast to android.graphics.Bitmap[]

I thought it was something to do with not properly setting the classLoader, but I tried everything and can't seem to get it to work

like image 316
Cigogne Eveillée Avatar asked Dec 09 '25 00:12

Cigogne Eveillée


1 Answers

Hi It is not recommended to put the Bitmap in a bundle that cost extra processing time. And consumes lot of memory.. Highly recommended to pass the path or save the image in the External storage and and bundle the absolute path to the image file....check this question in SO Bundle Bitmap .

like image 108
Pragnani Avatar answered Dec 11 '25 14:12

Pragnani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!