Reputation: 316
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?
Upvotes: 1
Views: 946
Reputation: 2288
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);
}
Upvotes: 3