1

I need to pass from one Activity to other a list of news (it's a RSS reader) for to display them in a ViewPager.

All data about news is stored in a class:

public class SingleNew implements java.io.Serializable{

    public String title;
    public String link;
    public Date date;
    public Bitmap image;

}

So, as in the first activity I have already downloaded all news with images included, to avoid download again in the other activity, I decided to pass them on an Intent as parcelable object. So I created MyCustomParcelable.

public class CustomParcelable implements Parcelable {

    public List<SingleNew> news;

    public CustomParcelable(){
        news= new ArrayList<SingleNew>();
    }

    public CustomParcelable(Parcel in) {
        news= new ArrayList<SingleNew>();
        readFromParcel(in);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
         dest.writeList(news);
    }

    private void readFromParcel(Parcel in) {
        news = new ArrayList<SingleNew>();
        in.readList(news, List.class.getClassLoader());
    }

    public static final Parcelable.Creator CREATE = new Parcelable.Creator() {

         public CustomParcelable createFromParcel(Parcel parcel) {
              return new CustomParcelable(parcel);
         }

         public CustomParcelable[] newArray(int size) {
              return new CustomParcelable[size];
         }


    };
}

When I do startActivity() my console throws this message because of Bitmaps needs a special deal.

Caused by: java.io.NotSerializableException: android.graphics.Bitmap

I know how to pass a single bitmap as a one property of my CustomParcelable object but in this case I have a List<SingleNew> that inside of each one has Bitmap so I don't know how to prepare that.

I have found other questions about bitmaps but as I said, explain how to pass as single property, not as a property of my custom object's list.

In addition I would ask if this is the best way to pass a huge amount of images. I read in other question that is more aproppiate store them in internal storage and then recover. Is it true? Or this way is correct?

korima
  • 306
  • 5
  • 12
  • I would rather use a String url and pass that to the other activity and load the image from there instead of using a Bitmap in your objects. Bitmaps are resource intensive and if you create a lot of objects containing them you will most likely run into memory problems. – Willie Nel Jul 30 '14 at 11:16

1 Answers1

3

use byte[] instand of Bitmap

use those functions to convert between Objects

 public static byte[] convert(Bitmap bitmap) throws IOException {
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG,100,stream);
    byte[] array = stream.toByteArray();
    stream.close();
    return array;


}
public static Bitmap convert(byte[] array) {
    return BitmapFactory.decodeByteArray(array,0,array.length);
} 
Itzik Samara
  • 2,278
  • 1
  • 14
  • 18
  • Thank you for answer so quickly. Now works fine! By the way, is it a good practice? I mean, pass several images using this way. Deppending of the website, I would read 20, 30 images maybe of high resolution and I don't know if it would crash. – korima Jul 30 '14 at 11:23
  • 1
    please mark as Answer if it helped to you Thank you .. no passing 20 , 30 images would cause OutOfMemory espically if there are heavy load images. cause everything allocating memory so low ends phones can stuck and crash. – Itzik Samara Jul 30 '14 at 11:24
  • It that case It would be better download again instead of passing as parcelables? – korima Jul 30 '14 at 11:33
  • 1
    serveral answers : 1.scale bitmap to 300x300 or lower depends on your usage usual there is no Tablet/Phone that uses very high scale images 2. downloading parts of News each time as your user navigate 3.save the images in the external storage ... again depends what is your proccess – Itzik Samara Jul 30 '14 at 11:38