views:

72

answers:

2

Hi, I want to serialize my class. I have this code :

public class Video implements Serializable{
public long videoId;
public String title;
public String publisher;
public String language;
public Date lastModified;
public Date published;
public String imageUrl;
public String url;
public Bitmap myVideoScreenshotBm;
//public Drawable myVideoScreenshotDrawable;

public CedemoVideo (long newVideoId) {
    this.videoId=newVideoId;
}

}

As far I have check i'm getting a NotSerializableException because of the Bitmap variable.

I apparently need to implement :

 private void writeObject(java.io.ObjectOutputStream out) throws IOException

private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException

Anyone know what I should do in these function ?? How should I write my Bitmap variable ?Any code examples ? anyone did it ?

+1  A: 

While serializing the bitmap variable, just serialize the url of the bitmap. Deserialization should make use of this url to construct the actual bitmap field.

chedine
Well, its exactly what im trying to avoid. I dont want to redownload the bitmap from internet. I want to save it and restore it when i need. How do I write a Bitmap object to an ObjectOutputStream ?
Fabien
Consider converting the bitmap to an array of bytes. Upon deserialization,read the byte array from the file system and construct the bitmap. Possible in android?
chedine
http://www.coderanch.com/t/449045/Android/Mobile/byte-image. May be this helps
chedine
thanks you all. I suceeded in serializing my bitmap.
Fabien
see : http://stackoverflow.com/questions/3628016/android-how-to-save-a-bitmap-buggy-code
Fabien
A: 

See here for a code example : http://stackoverflow.com/questions/3628016/android-how-to-save-a-bitmap-buggy-code

Fabien