Data storage (1) -- SharedPreferences: What you don't know, sharedpreferences
1. SharedPreferences: Save the data file to a specified path
SharedPreferences can only be stored in the private shared_prefs directory of the current application, but it is not absolute. We can use some unconventional methods to change the storage directory. reflection technology is a good choice.
First, implement the Code:
private SharedPreferences share;private SharedPreferences.Editor editor;
Key code for modifying the path:
private void initSharedPreferences(String path,String name,int mode){try { Field field =ContextWrapper.class.getDeclaredField("mBase");field.setAccessible(true);Object obj = field.get(this);field = obj.getClass().getDeclaredField("mPreferencesDir");field.setAccessible(true);File file = new File(path);field.set(obj, file);share = getSharedPreferences(name, mode);editor = share.edit();} catch (Exception e) {// TODO: handle exceptione.printStackTrace();}}
Field field =ContextWrapper.class.getDeclaredField("mBase");
Obtain the mBase variable in the ContextWrapper object, which saves the ContextImpl object, and the mPreferencesDir in the ContextImpl object saves the path of the data file.
share = getSharedPreferences(name, mode);
After this statement is executed, a file is created in the specified directory to save the data.
PS: Using Reflection technology, you need to carefully study the source code so that you can know where to modify and which variable to change.
Usage:
initSharedPreferences("/data/fly","config",Activity.MODE_PRIVATE);editor.putString("AA", "AAaa");editor.commit();Toast.makeText(this, share.getString("AA", ""), 1000).show();
Ii. SharedPreferences
In principle, SharedPreferences can only store strings in the form of key-value, but we can convert any binary data into strings by encoding, so that binary data can be saved in the SharedPreferences file, the most common encoding format is Base64.
private void saveDrawable(int id) { Bitmap bitmap = BitmapFactory.decodeResource(getResources(), id); ByteArrayOutputStream baos = new ByteArrayOutputStream(); bitmap.compress(CompressFormat.JPEG, 50, baos); String imageBase64 = new String(Base64.encodeToString(baos.toByteArray(),Base64.DEFAULT)); editor.putString("P",imageBase64 ); editor.commit(); } private Drawable loadDrawable() { String temp = share.getString("P", ""); ByteArrayInputStream bais = new ByteArrayInputStream(Base64.decode(temp.getBytes(), Base64.DEFAULT)); return Drawable.createFromStream(bais, ""); }
Iii. SharedPreferences
Since binary data can be stored as strings with SharedPreferences After encoding, it is also possible to save objects.
private void saveProduct(Product product){ByteArrayOutputStream baos = new ByteArrayOutputStream();try {ObjectOutputStream oos = new ObjectOutputStream(baos);oos.writeObject(product);String temp = new String(Base64.encode(baos.toByteArray(), Base64.DEFAULT));Log.i("AAA", temp);editor.putString("product", temp);editor.commit();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}private Product getProduct(){String temp = share.getString("product", "");ByteArrayInputStream bais = new ByteArrayInputStream(Base64.decode(temp.getBytes(), Base64.DEFAULT));Product product = null;try {ObjectInputStream ois = new ObjectInputStream(bais);product = (Product) ois.readObject();} catch (IOException e) {// TODO Auto-generated catch blockLog.i("AAA", e.toString());}catch(ClassNotFoundException e1){Log.i("AAA", e1.toString());}return product;}
The premise that an object can be stored by SharedPreferences is that the object is serialized. That is to say, to implement the Serializable interface, the Serializable interface is actually an empty interface, just to mark that the object is serialized.
public static class Product implements Serializable{String name;String id;int count;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getId() {return id;}public void setId(String id) {this.id = id;}public int getCount() {return count;}public void setCount(int count) {this.count = count;}public String toString(){return "name:"+name+" id:"+id+" count:"+count;}}
If the class is an internal class, you need to write it as static. Otherwise, java. io. NotSerializableException will occur. Solution: click here.
How to Make SharedPreferences save multiple groups of data
Change "Activity. MODE_PRIVATE" in "share = SharedPreferencesActivity. this. getSharedPreferences (FILENAME, Activity. MODE_PRIVATE);" to "Activity. MODE_APPEND!
How does Android SharedPreferences store data in one activity? How does one obtain the SharedPreferences data in another activity?
This is not difficult to implement. In another activity, call this. getSharedPreferences ('your sharedPreference name' int mode;