图片存储
try
{
SharedPreferences sharedPreferences = getSharedPreferences(
"base64", Activity.MODE_PRIVATE);
Editor editor = sharedPreferences.edit();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
BitmapFactory.decodeResource(getResources(), R.drawable.flower)
.compress(CompressFormat.JPEG, 50, baos);
String imageBase64 = new String(Base64.encode(baos.toByteArray(),
Base64.DEFAULT));
editor.putString("image", imageBase64);
editor.commit();
baos.close();
}
catch (Exception e)
{
// TODO: handle exception
}图片读取
try
{
Product product = new Product();
product.name = "Android手机";
product.price = 2800;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(product);
SharedPreferences sharedPreferences = getSharedPreferences(
"base64", Activity.MODE_PRIVATE);
Editor editor = sharedPreferences.edit();
sharedPreferences = getSharedPreferences("base64",
Activity.MODE_PRIVATE);
String productBase64 = new String(Base64.encode(baos.toByteArray(),
Base64.DEFAULT));
editor.putString("product", productBase64);
editor.commit();
oos.close();
}
catch (Exception e)
{
// TODO: handle exception
}对象存储
try
{
Product product = new Product();
product.name = "Android手机";
product.price = 2800;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(product);
SharedPreferences sharedPreferences = getSharedPreferences(
"base64", Activity.MODE_PRIVATE);
Editor editor = sharedPreferences.edit();
sharedPreferences = getSharedPreferences("base64",
Activity.MODE_PRIVATE);
String productBase64 = new String(Base64.encode(baos.toByteArray(),
Base64.DEFAULT));
editor.putString("product", productBase64);
editor.commit();
oos.close();
}
catch (Exception e)
{
// TODO: handle exception
}对象读取
try
{
SharedPreferences sharedPreferences = getSharedPreferences(
"base64", Activity.MODE_PRIVATE);
String base64Product = sharedPreferences.getString("product", "");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] productBytes = Base64.decode(base64Product.getBytes(),
Base64.DEFAULT);
ByteArrayInputStream bais = new ByteArrayInputStream(productBytes);
ObjectInputStream ois = new ObjectInputStream(bais);
Product product = (Product) ois.readObject();
Toast.makeText(this,
"name:" + product.name + "\nprice:" + product.price,
Toast.LENGTH_LONG).show();
ois.close();
}
catch (Exception e)
{
// TODO: handle exception
}
利用session进行图片与对象存储共享&spm=1001.2101.3001.5002&articleId=6884791&d=1&t=3&u=e17456ef400340fb8a72927920cc840e)
1505

被折叠的 条评论
为什么被折叠?



