我们知道SharedPreferences只能保存简单类型的数据,例如,String、int等。
如果想用SharedPreferences存取更复杂的数据类型(类、图像等),就需要对这些数据进行编码。
我们通常会将复杂类型的数据转换成Base64编码,然后将转换后的数据以字符串的形式保存在 XML文件中。
public void saveOAuth(OAuthV1 oAuth_1) {
SharedPreferences preferences = getSharedPreferences("base64",
MODE_PRIVATE);
// 创建字节输出流
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
// 创建对象输出流,并封装字节流
ObjectOutputStream oos = new ObjectOutputStream(baos);
// 将对象写入字节流
oos.writeObject(oAuth_1);
// 将字节流编码成base64的字符窜
String oAuth_Base64 = new String(Base64.encodeBase64(baos
.toByteArray()));
Editor editor = preferences.edit();
editor.putString("oAuth_1", oAuth_Base64);
editor.commit();
} catch (IOException e) {
// TODO Auto-generated
}
Log.i("ok", "存储成功");
}
之后将其解码:
public OAuthV1 readOAuth() {
OAuthV1 oAuth_1 = null;
SharedPreferences preferences = getSharedPreferences("base64",
MODE_PRIVATE);
String productBase64 = preferences.getString("oAuth_1", "");
//读取字节
byte[] base64 = Base64.decodeBase64(productBase64.getBytes());
//封装到字节流
ByteArrayInputStream bais = new ByteArrayInputStream(base64);
try {
//再次封装
ObjectInputStream bis = new ObjectInputStream(bais);
try {
//读取对象
oAuth_1 = (OAuthV1) bis.readObject();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (StreamCorruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return oAuth_1;
}
需要导入一个包:commons-codec-1.4.jar
本文资源,导入一个product对象和一个图片,代码如下:
package net.blogjava.mobile;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Field;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.codec.binary.Base64;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.res.TypedArray;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;
public class Main extends Activity implements OnClickListener
{
private SharedPreferences mySharedPreferences;
private EditText etProductID;
private EditText etProductName;
private EditText etProductPrice;
private ImageView imageView;
private List<Integer> imageResIdList = new ArrayList<Integer>();
public class ImageAdapter extends BaseAdapter
{
int mGalleryItemBackground;
private Context mContext;
public ImageAdapter(Context context)
{
mContext = context;
TypedArray typedArray = obtainStyledAttributes(R.styleable.Gallery1);
mGalleryItemBackground = typedArray.getResourceId(
R.styleable.Gallery1_android_galleryItemBackground, 0);
}
public int getCount()
{
return imageResIdList.size();
}
public Object getItem(int position)
{
return position;
}
public long getItemId(int position)
{
return position;
}
public View getView(int position, View convertView, ViewGroup parent)
{
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(imageResIdList.get(position));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(new Gallery.LayoutParams(136, 88));
imageView.setBackgroundResource(mGalleryItemBackground);
return imageView;
}
}
@Override
public void onClick(View view)
{
try
{
switch (view.getId())
{
case R.id.btnSave:
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);
oos.writeObject(product);
mySharedPreferences = getSharedPreferences("base64",
Activity.MODE_PRIVATE);
String productBase64 = new String(Base64.encodeBase64(baos
.toByteArray()));
SharedPreferences.Editor editor = mySharedPreferences
.edit();
editor.putString("product", productBase64);
baos = new ByteArrayOutputStream();
((BitmapDrawable) imageView.getDrawable()).getBitmap()
.compress(CompressFormat.JPEG, 50, baos);
String imageBase64 = new String(Base64.encodeBase64(baos
.toByteArray()));
editor.putString("productImage", imageBase64);
//这里可以看出,把对象product和图像都压缩进入了xml文件
editor.commit();
oos.close();
new AlertDialog.Builder(this).setTitle("保存成功.")
.setPositiveButton("确定", null).show();
break;
case R.id.btnSelectImage:
View myView = getLayoutInflater().inflate(R.layout.gallery,
null);
final Gallery gallery = (Gallery) myView
.findViewById(R.id.gallery);
ImageAdapter imageAdapter = new ImageAdapter(this);
gallery.setAdapter(imageAdapter);
new AlertDialog.Builder(this)
.setTitle("选择产品图像")
.setView(myView)
.setPositiveButton(
"确定",
new android.content.DialogInterface.OnClickListener()
{
@Override
public void onClick(
DialogInterface dialog,
int which)
{
imageView
.setImageResource(imageResIdList
.get(gallery
.getSelectedItemPosition()));
}
}).setNegativeButton("取消", null).show();
break;
}
}
catch (Exception e)
{
setTitle("error:" + e.getMessage());
}
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnSave = (Button) findViewById(R.id.btnSave);
Button btnSelectImage = (Button) findViewById(R.id.btnSelectImage);
etProductID = (EditText) findViewById(R.id.etProductID);
etProductName = (EditText) findViewById(R.id.etProductName);
etProductPrice = (EditText) findViewById(R.id.etProductPrice);
imageView = (ImageView) findViewById(R.id.imageview);
btnSave.setOnClickListener(this);
btnSelectImage.setOnClickListener(this);
byte[] base64Bytes;//进行一些初始化的操作
ByteArrayInputStream bais;
try
{
mySharedPreferences = getSharedPreferences("base64",
Activity.MODE_PRIVATE);
String productBase64 = mySharedPreferences.getString("product", "");
base64Bytes = Base64.decodeBase64(productBase64.getBytes());
bais = new ByteArrayInputStream(base64Bytes);
ObjectInputStream ois = new ObjectInputStream(bais);
Product product = (Product) ois.readObject();
etProductID.setText(product.getId());
etProductName.setText(product.getName());
etProductPrice.setText(String.valueOf(product.getPrice()));
ois.close();
}
catch (Exception e)
{
}
try
{
String imageBase64 = mySharedPreferences.getString("productImage",
"");
base64Bytes = Base64.decodeBase64(imageBase64.getBytes());
bais = new ByteArrayInputStream(base64Bytes);
imageView.setImageDrawable(Drawable.createFromStream(bais,
"product_image"));
Field[] fields = R.drawable.class.getDeclaredFields();
for (Field field : fields)
{
if (!"icon".equals(field.getName()))
imageResIdList.add(field.getInt(R.drawable.class));
}
}
catch (Exception e)
{
// TODO: handle exception
}
}
}
项目整体代码如下:
http://download.csdn.net/detail/chengyangyy/8865847