我正在使用以下代码在 Android 内部存储器中保存和检索对象。我已经成功地使用字符串对象对其进行了测试。当我尝试保存位图时,我得到一个 IOException。(我发现我必须调用compress方法)所以我注意到我可以保存一些对象,保存对象有什么限制?
当我保存一个对象时,它是否只保存字段?我可以保存包含其他对象的对象吗?请把我清理干净。
代码:
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);
}
}
提前致谢。