SharedPreferences存储Bitmap图片 首先需要将图片写成字节流,转换为String字符串,将字符串存入SharedPreferences中。
1.存入
SharedPreferences sharedPre=getSharedPreferences("Image", MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPre.edit();
Bitmap bit = data.getParcelableExtra("data");
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bit.compress(Bitmap.CompressFormat.JPEG,50,byteArrayOutputStream);
String headimg = new String(Base64.encodeToString(byteArrayOutputStream.toByteArray(),Base64.DEFAULT));
editor.putString("icon",headimg);
editor.commit();
2.取出
Bitmap bitmap = null;
SharedPreferences sharedPre=getSharedPreferences("Image", MODE_PRIVATE);
String icon = sharedPre.getString("icon", "");
if(icon != "") {
byte[] decode = Base64.decode(icon.getBytes(), 1);
bitmap = BitmapFactory.decodeByteArray(decode, 0, decode.length);
imageView.setImageBitmap(bitmap);
}