Android SharedPreferences存图片,转码解码图片

public void disposeImage()
{
ByteArrayOutputStream outputStream = null;
try
{
SharedPreferences preferences = getSharedPreferences(SHARED_PREFERENCES_NAME, Activity.MODE_PRIVATE);
Editor editor = preferences.edit();
outputStream = new ByteArrayOutputStream();
/*
* 读取和压缩图片资源 并将其保存在 ByteArrayOutputStream对象中
*/
BitmapFactory.decodeResource(getResources(), R.drawable.jt6).compress(CompressFormat.JPEG, 50, outputStream);
String imgBase64 = new String(Base64.encode(outputStream.toByteArray(), Base64.DEFAULT));
editor.putString("image", imgBase64);
editor.commit();

readImage();
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
if (null != outputStream)
{
try
{
outputStream.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}

public void readImage()
{
ByteArrayInputStream inputStream = null;
try
{
SharedPreferences preferences = getSharedPreferences(SHARED_PREFERENCES_NAME, Activity.MODE_PRIVATE);
String imgbase64 = preferences.getString("image", "");
byte[] imgbyte = Base64.decode(imgbase64.getBytes(), Base64.DEFAULT);
inputStream = new ByteArrayInputStream(imgbyte);
ImageView view = (ImageView) findViewById(R.id.preference_image);
view.setImageDrawable(Drawable.createFromStream(inputStream, "image"));
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
if (null != inputStream)
{
try
{
inputStream.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值