標籤:
我們知道SharedPreferences簡單類型的資料。比如。String、int等。
假設想用SharedPreferences存取更複雜的資料類型(類、映像等),就須要對這些資料進行編碼。
我們一般會將複雜類型的資料轉換成Base64編碼,然後將轉換後的資料以字串的形式儲存在 XML檔案裡。
Android SDK中並未提供Base64編碼和解碼庫。
因此,須要使用第三方的jar包。
在本例中使用了Apache Commons組件集中的Codec組件進行Base64編碼和解碼。讀者能夠從例如以下的地址下載Codec組件的安裝包。
在Androidproject檔案夾的lib子檔案夾中已經包括了Codec組件的jar包(commons-codec-1.4.jar)。因此,讀者能夠在該project中直接使用Codec組件。
在本例中將一個Product類的對象執行個體和一個映像儲存在XML檔案裡,並在程式又一次執行後從XML檔案裝載Product對象和映像。
以下是Product類的代碼:
java代碼:
package eoe.mobile;import java.io.Serializable;// 須要序列化的類必須實現Serializable介面public class Product implements Serializable{private String id;private String name;private float price;<span style="font-family:Microsoft Yahei, Tahoma, Simsun;color:#444444;"><span style="font-size: 14px; font-weight: 700; line-height: 21px;"></span></span>
在存取資料之前。須要使用以下的代碼建立一個SharedPreferences對象。
mySharedPreferences = getSharedPreferences("base64",Activity.MODE_PRIVATE);當中mySharedPreferences是在類中定義的SharedPreferences類型變數。
在儲存Product對象之前,須要建立Product對象,並將對應組件中的值賦給Product類的對應屬性。將Product對象儲存在XML檔案裡的代碼例如以下:
java代碼:
Product product = new Product();product.setId(etProductID.getText().toString());product.setName(etProductName.getText().toString());product.setPrice(Float.parseFloat(etProductPrice.getText().toString()));ByteArrayOutputStream baos = new ByteArrayOutputStream();ObjectOutputStream oos = new ObjectOutputStream(baos);// 將Product對象放到OutputStream中oos.writeObject(product);mySharedPreferences = getSharedPreferences("base64", Activity.MODE_PRIVATE);// 將Product對象轉換成byte數組,並將其進行base64編碼String productBase64 = new String(Base64.encodeBase64(baos.toByteArray()));SharedPreferences.Editor editor = mySharedPreferences.edit();// 將編碼後的字串寫到base64.xml檔案裡editor.putString("product", productBase64);editor.commit();
儲存映像的方法與儲存Product對象的方法類似。因為在儲存之前,須要選擇一個映像,並將該映像顯示在ImageView組件中,因此。從ImageView組件中能夠直接獲得要儲存的映像。
將圖象儲存在XML檔案裡的代碼例如以下:
java代碼:
ByteArrayOutputStream baos = new ByteArrayOutputStream();// 將ImageView組件中的映像壓縮成JPEG格式,並將壓縮結果儲存在ByteArrayOutputStream對象中((BitmapDrawable) imageView.getDrawable()).getBitmap().compress(CompressFormat.JPEG, 50, baos);String imageBase64 = new String(Base64.encodeBase64(baos.toByteArray()));// 儲存由映像位元組流轉換成的Base64格式字串editor.putString("productImage", imageBase64);editor.commit();
當中compress方法的第2個參數表示壓縮品質,取值範圍是0至100,0表示最高壓縮比,但映像效果最差,100則恰恰相反。
在本例中取了一個中間值50。
從XML檔案裡裝載Product對象和映像是儲存的逆過程。
也就是從XML檔案裡讀取Base64格式的字串,然後將其解碼成位元組數組。最後將位元組數群組轉換成Product和Drawable對象。裝載Product對象的代碼例如以下:
java代碼:
String productBase64 = mySharedPreferences.getString("product", "");// 對Base64格式的字串進行解碼byte[] base64Bytes = Base64.decodeBase64(productBase64.getBytes());ByteArrayInputStream bais = new ByteArrayInputStream(base64Bytes);ObjectInputStream ois = new ObjectInputStream(bais);// 從ObjectInputStream中讀取Product對象Product product = (Product) ois.readObject();<span style="font-family:Microsoft Yahei, Tahoma, Simsun;color:#444444;"><span style="font-size: 14px; font-weight: 700; line-height: 21px;"></span></span>
裝載映像的代碼例如以下:
java代碼:
String imageBase64 = mySharedPreferences.getString("productImage","");base64Bytes = Base64.decodeBase64(imageBase64.getBytes());bais = new ByteArrayInputStream(base64Bytes);// 在ImageView組件上顯示映像imageView.setImageDrawable(Drawable.createFromStream(bais,"product_image"));<span style="font-family:Microsoft Yahei, Tahoma, Simsun;color:#444444;"><span style="font-size: 14px; font-weight: 700; line-height: 21px;"></span></span>
在上面的代碼中使用了Drawable類的createFromStream方法直接從流建立了Drawable對象,並使用setImageDrawable映像顯示的在該方法中ImageView組件。
著作權聲明:本文博主原創文章。部落格,未經同意,不得轉載。
Android SharedPreferences複雜的儲存