I'm using the following code to save and retrieve objects to and from the Androids internal memory. I have tested it with a string object with success. When I try to save a Bitmap I get a IOException. (I found out that I have to call the compress method) So I notice that I can save some objects, what are the restrictions to saving objects?
When I save a object, does it only save the fields? Can I save objects that contains other objects? Please clear me up.
Code:
public Object readObjectFromMemory(String filename) {
Object defautObject = null;
FileInputStream fis;
try {
fis = game.openFileInput(filename);
ObjectInputStream is = new ObjectInputStream(fis);
defautObject = is.readObject();
is.close();
this.gameEngineLog.d(classTAG, "Object successfully read: " + filename);
}
catch (FileNotFoundException e) {
e.printStackTrace();
this.gameEngineLog.d(classTAG, "FileNotFoundException");
this.gameEngineLog.d(classTAG, "Object couldn't be opened: " + filename);
}
catch (StreamCorruptedException e) {
e.printStackTrace();
this.gameEngineLog.d(classTAG, "StreamCorruptedException");
this.gameEngineLog.d(classTAG, "Object couldn't be opened: " + filename);
}
catch (IOException e) {
e.printStackTrace();
this.gameEngineLog.d(classTAG, "IOException");
this.gameEngineLog.d(classTAG, "Object couldn't be opened: " + filename);
}
catch (ClassNotFoundException e) {
e.printStackTrace();
this.gameEngineLog.d(classTAG, "ClassNotFoundException");
this.gameEngineLog.d(classTAG, "Object couldn't be opened: " + filename);
}
return defautObject;
}
public void writeObjectToMemory(String filename, Object object) {
FileOutputStream fos;
try {
fos = game.openFileOutput(filename, Context.MODE_PRIVATE);
ObjectOutputStream os = new ObjectOutputStream(fos);
os.writeObject(object);
os.close();
this.gameEngineLog.d(classTAG, "Object successfully written: " + filename);
}
catch (FileNotFoundException e) {
e.printStackTrace();
this.gameEngineLog.d(classTAG, "Object couldn't be written: " + filename);
}
catch (IOException e) {
e.printStackTrace();
this.gameEngineLog.d(classTAG, "Object couldn't be written: " + filename);
}
}
Thanks in advance.