我尝试在mysql中保存SharedPreferences
并恢复它。我使用以下代码存储SharedPreferences
:
private boolean saveSharedPreferencesToFile(File dst) {
boolean res = false;
ObjectOutputStream output = null;
try {
output = new ObjectOutputStream(new FileOutputStream(dst));
SharedPreferences pref = getSharedPreferences("AnimeSAShardPref", MODE_PRIVATE);
output.writeObject(pref.getAll());
res = true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if (output != null) {
output.flush();
output.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return res;
}
创建文件后,我读取文件并将其上传到mysql,没关系。但是当我想从mysql中获取数据以使其成为文件时我可以使用:
@SuppressWarnings({ "unchecked" })
private boolean loadSharedPreferencesFromFile(File src) {
boolean res = false;
ObjectInputStream input = null;
try {
input = new ObjectInputStream(new FileInputStream(src));
SharedPreferences.Editor prefEdit = getSharedPreferences("sssss", MODE_PRIVATE).edit();
prefEdit.clear();
Map<String, ?> entries = (Map<String, ?>) input.readObject();
for (Map.Entry<String, ?> entry : entries.entrySet()) {
Object v = entry.getValue();
String key = entry.getKey();
if (v instanceof Boolean)
prefEdit.putBoolean(key, ((Boolean) v));
else if (v instanceof Float)
prefEdit.putFloat(key, ((Float) v));
else if (v instanceof Integer)
prefEdit.putInt(key, ((Integer) v));
else if (v instanceof Long)
prefEdit.putLong(key, ((Long) v));
else if (v instanceof String)
prefEdit.putString(key, ((String) v));
}
prefEdit.apply();
res = true;
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
if (input != null) {
input.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return res;
}
我收到此错误:
04-30 10:45:00.910 24101-24101/com.test W/System.err﹕ java.io.StreamCorruptedException
04-30 10:45:00.910 24101-24101/com.test W/System.err﹕ at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:2067)
04-30 10:45:00.910 24101-24101/com.test W/System.err﹕ at java.io.ObjectInputStream.<init>(ObjectInputStream.java:372)
04-30 10:45:00.910 24101-24101/com.test W/System.err﹕ at com.test.LoginActivity.loadSharedPreferencesFromFile(LoginActivity.java:347)
04-30 10:45:00.910 24101-24101/com.test W/System.err﹕ at com.test.LoginActivity.access$300(LoginActivity.java:57)
04-30 10:45:00.910 24101-24101/com.test W/System.err﹕ at com.test.LoginActivity$DownloadFileFromURL.onPostExecute(LoginActivity.java:337)
04-30 10:45:00.910 24101-24101/com.test W/System.err﹕ at com.test.LoginActivity$DownloadFileFromURL.onPostExecute(LoginActivity.java:303)
04-30 10:45:00.920 24101-24101/com.test W/System.err﹕ at android.os.AsyncTask.finish(AsyncTask.java:632)
04-30 10:45:00.920 24101-24101/com.test W/System.err﹕ at android.os.AsyncTask.access$600(AsyncTask.java:177)
04-30 10:45:00.920 24101-24101/com.test W/System.err﹕ at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
04-30 10:45:00.920 24101-24101/com.test W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:102)
04-30 10:45:00.920 24101-24101/com.test W/System.err﹕ at android.os.Looper.loop(Looper.java:146)
04-30 10:45:00.920 24101-24101/com.test W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5593)
04-30 10:45:00.920 24101-24101/com.test W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
04-30 10:45:00.920 24101-24101/com.test W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515)
04-30 10:45:00.920 24101-24101/com.test W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
04-30 10:45:00.920 24101-24101/com.test W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
04-30 10:45:00.920 24101-24101/com.test W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)