Cigogne  Eveillée
Cigogne Eveillée

Reputation: 2208

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

Upvotes: 3

Views: 2853

Answers (2)

Pragnani
Pragnani

Reputation: 20155

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 .

Upvotes: 3

smk
smk

Reputation: 5882

You can do something like this.Where p is your parcelable list. And finally convert list of bitmaps back to an array.

List<Parcelable> pList = Arrays.asList(p);
        ArrayList<Bitmap> bList = new ArrayList<Bitmap>();
        for(Parcelable px : pList){
            bList.add((Bitmap)px);
        }

Upvotes: 1

Related Questions