I've been doing some work in Android, and I've found after three or four different attempts, I can't seem to get Android to save my file anywhere. In fact, it doesn't even create a folder in data/ for my app.
Here's how I'm trying to save the file - this is in my main activity also.
@Override
public void onStop(){
super.onStop();
try{
FileOutputStream memboricOut = getApplicationContext().openFileOutput("memboric.core", Context.MODE_PRIVATE);
brain.save(memboricOut);
}catch(Exception e){
System.err.println("Could not save memboric core.");
e.printStackTrace();
}
}
And inside brain.save():
public boolean save(FileOutputStream fileOut) {
try{
ObjectOutputStream oo = new ObjectOutputStream(fileOut);
oo.writeObject(this);
oo.close();
fileOut.close();
System.out.println("Memboric Core saved successfully.");
return true;
}catch (Exception e) {
e.printStackTrace();
}
return false;
}
This code seems to do nothing. I've also placed the saving in onDestroy as well.
Have I just chosen bad placement for the saving? I can't imagine what could possibly being going wrong.
Do you have the permission to save files ? You can add that by adding android.permission.WRITE_EXTERNAL_STORAGE permission to you AnroidManifest.xml.
oo.writeObject(this);
Is brain have serializable data?
Class Brain implements java.io.Serializable {
public String toString() {
return "serialized data";
}
}
If not, DataOutputStream maybe a better choice.
DataOutputStream oo = new DataOutputStream(fileOut);
oo.writeInt(value); // if breain have int value
oo.flush();
oo.close();
Saved data will be appear in /data/data/(your.package.name)/files/memboric.core
Check the file with emulator (or rooted device).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.