[英]Android Not Saving Files
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. 我一直在Android上做一些工作,经过三到四次不同的尝试,我发现似乎无法让Android将文件保存在任何地方。 In fact, it doesn't even create a folder in data/ for my app.
实际上,它甚至没有为我的应用程序在data /中创建一个文件夹。
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(): 在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.
我也将保存的内容也放在了onDestroy中。
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.
您可以通过向AnroidManifest.xml添加android.permission.WRITE_EXTERNAL_STORAGE权限来添加该权限。
oo.writeObject(this);
oo.writeObject(本);
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可能是一个更好的选择。
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 保存的数据将显示在/data/data/(your.package.name)/files/memboric.core中
Check the file with emulator (or rooted device). 使用仿真器(或根设备)检查文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.