3

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

i_am_jorf
  • 53,608
  • 15
  • 131
  • 222
Cigogne Eveillée
  • 2,178
  • 22
  • 36

2 Answers2

3

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 .

Community
  • 1
  • 1
Pragnani
  • 20,075
  • 6
  • 49
  • 74
2

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);
        }
smk
  • 5,340
  • 5
  • 27
  • 41
  • I'll give it a shot. Any explanation though on what's wrong with my method? It works just fine until the process is killed – Cigogne Eveillée Feb 22 '13 at 04:06
  • Yep, app doesn't crash because of the exception anymore. This SO answer has a good explanation why: [link](http://stackoverflow.com/questions/8745893/i-dont-get-why-this-classcastexception-occurs?lq=1) @Pragnani 's recommendation is probably best to follow though for large bitmaps – Cigogne Eveillée Feb 23 '13 at 01:58