我需要将此对象存储到手机的内部存储内存中,并且我在清单上设置了<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE"></uses-permission>
。对象本身有两个静态方法用于存储并从内部存储器重新加载它:
public class SaveState implements Serializable {
static public List<FullMagazine> fms = new ArrayList<FullMagazine>();
static SaveState instance=null;
public static SaveState getInstance(){
if( instance == null )
instance = new SaveState();
return instance;
}
public static void saveData(SaveState instance){
ObjectOutput out;
try {
out = new ObjectOutputStream(new FileOutputStream("appSaveState.data"));
out.writeObject(instance);
out.close();
} catch (Exception e) {e.printStackTrace();}
}
public static SaveState loadData(){
ObjectInput in;
SaveState ss=null;
try {
in = new ObjectInputStream(new FileInputStream("appSaveState.data"));
ss=(SaveState) in.readObject();
in.close();
} catch (Exception e) {e.printStackTrace();}
return ss;
}
}
我正在尝试存储它并在我的活动中使用此调用重新打开它:
SaveState.fms.add(fm);
SaveState.saveData(SaveState.getInstance());
SaveState sv = SaveState.loadData();
但它不起作用,它不存储对象,当然它不是在读取objecto,我在存储和读取对象时遇到了这两个异常:
03-23 09:18:16.702: WARN/System.err(9060): java.io.FileNotFoundException: /appSaveState.data (Read-only file system)
03-23 09:18:16.702: WARN/System.err(9060): at org.apache.harmony.luni.platform.OSFileSystem.openImpl(Native Method)
03-23 09:18:16.702: WARN/System.err(9060): at org.apache.harmony.luni.platform.OSFileSystem.open(OSFileSystem.java:152)
03-23 09:18:16.702: WARN/System.err(9060): at java.io.FileOutputStream.<init>(FileOutputStream.java:97)
03-23 09:18:16.702: WARN/System.err(9060): at java.io.FileOutputStream.<init>(FileOutputStream.java:168)
03-23 09:18:16.702: WARN/System.err(9060): at java.io.FileOutputStream.<init>(FileOutputStream.java:147)
03-23 09:18:16.702: WARN/System.err(9060): at com.Magazine.SaveState.saveData(SaveState.java:35)
03-23 09:18:16.702: WARN/System.err(9060): at com.Magazine.MainMenu$DownloadThread.run(MainMenu.java:807)
03-23 09:18:16.709: WARN/System.err(9060): java.io.FileNotFoundException: /appSaveState.data (No such file or directory)
03-23 09:18:16.709: WARN/System.err(9060): at org.apache.harmony.luni.platform.OSFileSystem.openImpl(Native Method)
03-23 09:18:16.709: WARN/System.err(9060): at org.apache.harmony.luni.platform.OSFileSystem.open(OSFileSystem.java:152)
03-23 09:18:16.709: WARN/System.err(9060): at java.io.FileInputStream.<init>(FileInputStream.java:82)
03-23 09:18:16.709: WARN/System.err(9060): at java.io.FileInputStream.<init>(FileInputStream.java:134)
03-23 09:18:16.709: WARN/System.err(9060): at com.Magazine.SaveState.loadData(SaveState.java:46)
03-23 09:18:16.709: WARN/System.err(9060): at com.Magazine.MainMenu$DownloadThread.run(MainMenu.java:809)
代码中有什么问题?
由于
答案 0 :(得分:5)
更改此
out = new ObjectOutputStream(new FileOutputStream("appSaveState.data"));
与
File outFile = new File(Environment.getExternalStorageDirectory(), "appSaveState.data");
out = new ObjectOutputStream(new FileOutputStream(outFile));
正如@ e-x正确指出的那样,不会删除文件清除应用程序的数据或卸载应用程序
答案 1 :(得分:2)
您不能只是写入内部存储空间(即使您获得了该权限)。 尝试使用外部SD或cache folder代替。
答案 2 :(得分:1)
根据这篇文章和this
,这是我的方法我在我的用户类
上写了这个private static File mFolder;
public void saveData(Activity pContext) {
//this could be initialized once onstart up
if(mFolder == null){
mFolder = pContext.getExternalFilesDir(null);
}
this.save();
ObjectOutput out;
try {
File outFile = new File(mFolder,
"someRandom.data");
out = new ObjectOutputStream(new FileOutputStream(outFile));
out.writeObject(this);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static User loadData(Context pContext) {
if(mFolder == null){
mFolder = pContext.getExternalFilesDir(null);
}
ObjectInput in;
User lUser = null;
try {
FileInputStream fileIn = new FileInputStream(mFolder.getPath() + File.separator + "someRandom.data");
in = new ObjectInputStream(fileIn);
lUser = (User) in.readObject();
in.close();
} catch (Exception e) {
e.printStackTrace();
}
if (lUser != null) {
lUser.save();
Log.d("current User: ", lUser.nickname);
} else {
Log.d("current User: ", "null");
}
return lUser;
}
编辑:
然后从我的活动中打电话
mUser = User.loadData(mSelf);
或
mUser = User.loadData(this);
mSelf将是我存储的实例
希望这会有所帮助:)