Android_数据存储——SharedPreferences

来源:互联网 发布:网络极品萝莉 萌小兔 编辑:程序博客网 时间:2024/05/16 16:23

说明:该文中内容来自书《Android开发权威指南》,我通过整理,作为笔记。

SharedPreferences 的基本用法

1、保存简单数据

SharedPreferences是属于键值对形式,使用SharedPreferences保存key-value对的步骤如下:

(1)使用Activity。getSharedPreferences方法获得SharedPreferences对象。其中存储键值对的文件名称由getSharedPreferences方法的第一个参数指定。

(2)使用SharedPreferences.edit方法获得SharedPreferences.Editor对象。

(3)使用SharedPreferences.Editor.putXxx方法保存key-value对。

(4)通过SharedPreferences.Editor.commit方法保存key-value对。commit方法相当于数据库事务中的提交操作,只有在事务结束后进行提交,才会将数据真正保存在数据库中。

下面代码保存了两个键值对:

//获得SharedPreferences对象(第一步)SharedPreferences sp_write = getSharedPreferences("test", Activity.MODE_PRIVATE);//获得SharedPreferences.Editor对象(第二步)SharedPreferences.Editor editor = sp_write.edit();//使用pubXxx方法保存数据(第三步)editor.putString("name", "李宁");editor.putString("habit", "Android、协作、旅游");//将数据保存在文件中(第四步)editor.commit();Toast toast = Toast.makeText(this, "写数据成功", Toast.LENGTH_LONG);toast.show();

读取这两个键值对的代码如下:

SharedPreferences sp_read = getSharedPreferences("test", Activity.MODE_PRIVATE);String name = sp_read.getString("name", "");String habit = sp_read.getString("habit", "");Toast.makeText(this, "name:"+name+"\n"+"habit:"+habit, Toast.LENGTH_LONG).show();

实际上,SharedPreferences将数据文件写在手机内存(Rom)私有的目录中。在模拟中测试程序,可以通过DDMS透视图,静如“File Explorer”视图,找到data/data目录,通过相应的包名找到对应的文件。

2、保存复杂数据

SharedPreferences保存复杂数据,如图片,对象时,需要对这些复杂数据进行编码。通常会将复杂数据转换成Base64格式的编码,然后将转换后的数据以字符串的形式保存在xml文件中。

如下代码是保存图片:

try{SharedPreferences sp_store_image = getSharedPreferences("base64",Activity.MODE_PRIVATE);SharedPreferences.Editor editor = sp_store_image.edit();ByteArrayOutputStream baos = new ByteArrayOutputStream();// 读取和压缩图片,并将其压缩结果保存在ByteArrayOutputStream对象中BitmapFactory.decodeResource(getResources(), R.drawable.flower).compress(CompressFormat.JPEG, 50, baos);// 对压缩后的字节进行base64编码String imageBase64 = new String(Base64.encode(baos.toByteArray(), Base64.DEFAULT));// 保存转换后的Base64格式字符editor.putString("image", imageBase64);editor.commit();baos.close();} catch (Exception e){// TODO Auto-generated catch blocke.printStackTrace();}

读取图片:

try{SharedPreferences sp_read_image = getSharedPreferences("base64", Activity.MODE_PRIVATE);//读取Base64格式的图像数据String imageString = sp_read_image.getString("image", "");//对Base64格式的字符串进行解码,还原成字节数组byte[] imageBytes = Base64.decode(imageString.getBytes(), Base64.DEFAULT);ByteArrayInputStream bais = new ByteArrayInputStream(imageBytes);ImageView imageView = (ImageView) findViewById(R.id.image);imageView.setImageDrawable(Drawable.createFromStream(bais, "imageaa"));bais.close();} catch (IOException e){// TODO Auto-generated catch blocke.printStackTrace();}

保存对象:

try{Product product = new Product();product.name = "android手机";product.price = 2400;ByteArrayOutputStream baos = new ByteArrayOutputStream();//将对象Product保存在ObjectOutputStream对象中ObjectOutputStream oos = new ObjectOutputStream(baos);oos.writeObject(product);SharedPreferences sp_store_object = getSharedPreferences("base64", Activity.MODE_PRIVATE);SharedPreferences.Editor editor = sp_store_object.edit();//将对象Product转换成byte数组,将其进行base64编码String productBase64 = new String (Base64.encode(baos.toByteArray(), Base64.DEFAULT));//编码后的字符串保存在base64.xml文件中editor.putString("product", productBase64);editor.commit();baos.close();} catch (IOException e){// TODO Auto-generated catch blocke.printStackTrace();}

读取对象:

try{SharedPreferences sp_read_object = getSharedPreferences("base64", Activity.MODE_PRIVATE);//从base64.xml文件中读取Product对象的base64格式字符串String base64Product = sp_read_object.getString("product", "");//将base64格式字符串还原成byte数组byte[] productBytes = Base64.decode(base64Product.getBytes(), Base64.DEFAULT);ByteArrayInputStream bais = new ByteArrayInputStream(productBytes);ObjectInputStream ois = new ObjectInputStream(bais);//将byte数组转换成product对象Product product = (Product) ois.readObject();Toast.makeText(this, "name:"+product.name+"\nprice:"+product.price, Toast.LENGTH_LONG).show();ois.close();} catch (StreamCorruptedException e){// TODO Auto-generated catch blocke.printStackTrace();} catch (OptionalDataException e){// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e){// TODO Auto-generated catch blocke.printStackTrace();} catch (ClassNotFoundException e){// TODO Auto-generated catch blocke.printStackTrace();}
效果图如下:

如需源代码:

下载链接:
http://download.csdn.net/detail/zlfxy/5096301

原创粉丝点击