Android應用開發之使用SharedPreferences存儲復雜類型的數據


  上一篇文章寫了,關於使用SharedPreferences存儲簡單類型數據,下面再看看如何使用SharedPreferences存儲稍微復雜點的數據。這些數據包括圖片數據和對象或其它數據類型。存儲這類數據,需要將其編碼,將這些復雜類型的數據轉換成Base64格式的編碼,然后以字符串的形式保存在xml文件中。

源代碼(布局文件未貼出):

查看源代碼
 1 package android.test.sharedpreferencescomplex;
 2 
 3 import java.io.Serializable;
 4 
 5 public class MobileInfo implements Serializable {
 6 
 7 private static final long serialVersionUID = 1L;
 8 public String name;
 9 public String infoString;
10 
11 }
12 
13 
14 
15 package android.test.sharedpreferencescomplex;
16 
17 import java.io.ByteArrayInputStream;
18 import java.io.ByteArrayOutputStream;
19 import java.io.ObjectInputStream;
20 import java.io.ObjectOutputStream;
21 import android.os.Bundle;
22 import android.app.Activity;
23 import android.content.SharedPreferences;
24 import android.content.SharedPreferences.Editor;
25 import android.graphics.Bitmap.CompressFormat;
26 import android.graphics.BitmapFactory;
27 import android.graphics.drawable.Drawable;
28 import android.util.Base64;
29 import android.view.Menu;
30 import android.view.View;
31 import android.widget.ImageView;
32 import android.widget.Toast;
33 
34 public class MainActivity extends Activity {
35 
36 @Override
37 public void onCreate(Bundle savedInstanceState) {
38 super.onCreate(savedInstanceState);
39 setContentView(R.layout.activity_main);
40 }
41 
42 
43 public void onclick_Write_Image(View v) throws Throwable {
44 SharedPreferences sharedPreferences = getSharedPreferences("complex", Activity.MODE_PRIVATE);
45 Editor editor = sharedPreferences.edit();
46 ByteArrayOutputStream byteArrayOutputStream =new ByteArrayOutputStream();
47 BitmapFactory.decodeResource(getResources(), R.drawable.image1).compress(CompressFormat.JPEG, 50, byteArrayOutputStream);
48 String imageString = new String(Base64.encode(byteArrayOutputStream.toByteArray(),Base64.DEFAULT));
49 editor.putString("image", imageString);
50 editor.commit();
51 byteArrayOutputStream.close();    
52 }
53 
54 public void onclick_Read_Image(View view) throws Throwable {
55 SharedPreferences sharedPreferences = getSharedPreferences("complex",Activity.MODE_PRIVATE);
56 String string = sharedPreferences.getString("image", "");
57 byte[] imageBytes = Base64.decode(string.getBytes(), Base64.DEFAULT);
58 ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(imageBytes);
59 ImageView imageView =(ImageView)findViewById(R.id.imageView1);
60 imageView.setImageDrawable(Drawable.createFromStream(byteArrayInputStream, "image"));
61 byteArrayInputStream.close();
62 
63 }
64 public void onclick_Write_Data(View view) throws Throwable
65 {
66 MobileInfo mobile = new MobileInfo();
67 
68 mobile.name = "魅族";
69 mobile.infoString = "魅族MX";
70 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
71 ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
72 objectOutputStream.writeObject(mobile);
73 SharedPreferences sharedPreferences = getSharedPreferences("complex", Activity.MODE_PRIVATE);
74 Editor editor = sharedPreferences.edit();
75 sharedPreferences = getSharedPreferences("complex",Activity.MODE_PRIVATE);
76 String mobilesString = new String(Base64.encode(byteArrayOutputStream.toByteArray(),Base64.DEFAULT));
77 editor.putString("mobile", mobilesString);
78 editor.commit();
79 objectOutputStream.close();
80 }
81 
82 public void onclick_Read_Data(View view) throws Throwable, Throwable
83 {
84 
85 SharedPreferences sharedPreferences = getSharedPreferences("complex", Activity.MODE_PRIVATE);
86 String mobilesString = sharedPreferences.getString("mobile", "");
87 byte[] mobileBytes = Base64.decode(mobilesString.getBytes(),Base64.DEFAULT);
88 ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(mobileBytes);
89 ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
90 MobileInfo mobileInfo = (MobileInfo) objectInputStream.readObject();
91 Toast.makeText(this,"手機品牌:" + mobileInfo.name + "\n手機型號:" + mobileInfo.infoString,
92 Toast.LENGTH_LONG).show();
93 objectInputStream.close();
94 
95 
96 }
97 
98 }

 

  一.保存圖片文件

  首先,將這些數據類型編碼成Base64格式,實例化一個ByteArrayOutputStream對象,原來裝載壓縮后的字節文件。很多人接觸ByteArrayOutputStream時不了解,為什么有了FlileOutputStream還需要ByteArrayOutputStream,這就是一個很好的例子。ByteArrayOutputStream用來裝載需要緩沖的字節文件,而不當成文件寫入磁盤。

  

  ByteArrayOutputStream byteArrayOutputStream =new ByteArrayOutputStream();

   //然后我們使用BitmapFactory獲得我們要壓縮的文件,再用compress方法壓縮並保存到byteArrayOutputStream。

  BitmapFactory.decodeResource(getResources(), R.drawable.image1).compress(CompressFormat.JPEG, 50, byteArrayOutputStream);

  //最后,用Base64.encode將字節文件轉換成Base64編碼保存在String中。

  String imageString = new String(Base64.encode(byteArrayOutputStream.toByteArray(),Base64.DEFAULT));

  byteArrayOutputStream.close();
  得到圖片的String后,我們就可以像上篇文章中一樣使用SharedPreferences保存這個數據了。

  //下面代碼將告訴我們如何從文件中讀取保存的圖片信息。

  //定義一個byte類型的數組,對Base64格式的字符串進行解碼,還原成字節數組。
  byte[] imageBytes = Base64.decode(string.getBytes(), Base64.DEFAULT);

  //然后講imageBytes裝載到ByteArrayInputStream。

  ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(imageBytes);

  //最后,用Drawable的createFromSteam方法將byteArrayInputStream編譯成圖片文件。 

  ImageView imageView =(ImageView)findViewById(R.id.imageView1);

  imageView.setImageDrawable(Drawable.createFromStream(byteArrayInputStream, "image"));

  byteArrayInputStream.close();

 

  二.保存對象

  建立一個類,實例化一個對象

  public class MobileInfo implements Serializable {
  //該類實現Serializable接口,以啟用其序列化功能
  private static final long serialVersionUID = 1L;
  public String name;
  public String infoString;

  }

  MobileInfo mobile = new MobileInfo();

  mobile.name = "魅族";
  mobile.infoString = "魅族MX";

  //和保存圖片一樣,首先要將數據編碼成Base64格式,實例化一個ByteArrayOutputStream對象,原來裝載壓縮后的字節文件。

  ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

  //然后將得到的字符數據裝載到ObjectOutputStream。

  ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);

  //writeObject 方法負責寫入特定類的對象的狀態,以便相應的 readObject 方法可以還原它。
  objectOutputStream.writeObject(mobile);
  //最后,用Base64.encode將字節文件轉換成Base64編碼保存在String中
  String mobilesString = new String(Base64.encode(byteArrayOutputStream.toByteArray(),Base64.DEFAULT));
  //關閉objectOutputStream
  objectOutputStream.close();

 


  //下面代碼將告訴我們如何從文件中讀取保存的對象,參考圖片的讀取方法,相信大家能看懂,我就不再解釋了
  byte[] mobileBytes = Base64.decode(mobilesString.getBytes(),Base64.DEFAULT);
  ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(mobileBytes);
  ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
  MobileInfo mobileInfo = (MobileInfo) objectInputStream.readObject();
  objectInputStream.close();

 

                                      

                   圖.程序運行結果


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM