I created a class, which has several member variables, all of which are serializable... except one Bitmap! I tried to extend bitmap and implement serializable, not thinking
How about replacing Bitmap with a class like this:
public class SerialBitmap implements Serializable {
public Bitmap bitmap;
// TODO: Finish this constructor
SerialBitmap() {
// Take your existing call to BitmapFactory and put it here
bitmap = BitmapFactory.decodeSomething();
}
// Converts the Bitmap into a byte array for serialization
private void writeObject(java.io.ObjectOutputStream out) throws IOException {
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 0, byteStream);
byte bitmapBytes[] = byteStream.toByteArray();
out.write(bitmapBytes, 0, bitmapBytes.length);
}
// Deserializes a byte array representing the Bitmap and decodes it
private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
int b;
while((b = in.read()) != -1)
byteStream.write(b);
byte bitmapBytes[] = byteStream.toByteArray();
bitmap = BitmapFactory.decodeByteArray(bitmapBytes, 0, bitmapBytes.length);
}
}
The overridden Serializable.writeObject() and readObject() methods serialize the bytes instead of the Bitmap so the class is serializable. You will need to finish the constructor because I don't know how you currently construct your Bitmap. The last thing to do is to replace references to YourClass.bitmap with YourClass.serialBitmap.bitmap.
Good luck!
Barry P.S. This code compiles but I haven't tested it with a real bitmap