Android – 带有可序列化对象的SharedPreferences

我知道SharedPreferences有putFloat()putLong() putString()putFloat()putLong() putInt()putBoolean() 。 但是我需要在SharedPreferences存储Serializabletypes的对象。 我怎样才能做到这一点?

总之,你不能尝试序列化你的对象到一个私人文件,它相当于同样的事情。 样本类如下:

 import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import android.app.Activity; import android.content.Context; /** * * Writes/reads an object to/from a private local file * * */ public class LocalPersistence { /** * * @param context * @param object * @param filename */ public static void witeObjectToFile(Context context, Object object, String filename) { ObjectOutputStream objectOut = null; try { FileOutputStream fileOut = context.openFileOutput(filename, Activity.MODE_PRIVATE); objectOut = new ObjectOutputStream(fileOut); objectOut.writeObject(object); fileOut.getFD().sync(); } catch (IOException e) { e.printStackTrace(); } finally { if (objectOut != null) { try { objectOut.close(); } catch (IOException e) { // do nowt } } } } /** * * @param context * @param filename * @return */ public static Object readObjectFromFile(Context context, String filename) { ObjectInputStream objectIn = null; Object object = null; try { FileInputStream fileIn = context.getApplicationContext().openFileInput(filename); objectIn = new ObjectInputStream(fileIn); object = objectIn.readObject(); } catch (FileNotFoundException e) { // Do nothing } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } finally { if (objectIn != null) { try { objectIn.close(); } catch (IOException e) { // do nowt } } } return object; } } 

如果你的对象是简单的POJO,你可以把对象转换成JSONstring,并用putString()把它保存到共享的首选项中。

没有文件就可以做到。

我将这些信息序列化到base64,并且像这样我可以把它作为string保存在首选项中。

下面的代码将序列化的对象序列化为base64string,反之亦然:import android.util.Base64;

 import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; public class ObjectSerializerHelper { static public String objectToString(Serializable object) { String encoded = null; try { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream); objectOutputStream.writeObject(object); objectOutputStream.close(); encoded = new String(Base64.encodeToString(byteArrayOutputStream.toByteArray(),0)); } catch (IOException e) { e.printStackTrace(); } return encoded; } @SuppressWarnings("unchecked") static public Serializable stringToObject(String string){ byte[] bytes = Base64.decode(string,0); Serializable object = null; try { ObjectInputStream objectInputStream = new ObjectInputStream( new ByteArrayInputStream(bytes) ); object = (Serializable)objectInputStream.readObject(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (ClassCastException e) { e.printStackTrace(); } return object; } } 

被接受的答案是误导,我们可以使用GSON将可序列化对象存储到SharedPreferences中。 在google-gson阅读更多关于它的信息 。

你可以在Gradle文件中添加GSON依赖:

 compile 'com.google.code.gson:gson:2.7' 

这里的片段:

首先,创build你惯用的sharedPreferences:

 //Creating a shared preference SharedPreferences mPrefs = getPreferences(MODE_PRIVATE); 

从可序列化对象保存到首选项:

  Editor prefsEditor = mPrefs.edit(); Gson gson = new Gson(); String json = gson.toJson(YourSerializableObject); prefsEditor.putString("SerializableObject", json); prefsEditor.commit(); 

从首选项获取可序列化的对象:

 Gson gson = new Gson(); String json = mPrefs.getString("SerializableObject", ""); yourSerializableObject = gson.fromJson(json, YourSerializableObject.class);