您的位置:首页 > 移动开发 > Android开发

Android将图片转为字节流存储在SharedPreferences

2017-01-22 11:09 351 查看
public static void putImageToShare(Context mContext, ImageView imageView) {
BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
Bitmap bitmap = drawable.getBitmap();
ByteArrayOutputStream byStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 80, byStream);
byte[] byteArray = byStream.toByteArray();
String imgString = new String(Base64.encodeToString(byteArray, Base64.DEFAULT));
ShareUtils.putString(mContext, "image_title", imgString);
L.e(imgString);
}

public static void getImageToShare(Context mContext, ImageView imageView) {
String imgString = ShareUtils.getString(mContext, "image_title", "");
if (!imgString.equals("")) {
byte[] byteArray = Base64.decode(imgString, Base64.DEFAULT);
ByteArrayInputStream byStream = new ByteArrayInputStream(byteArray);
Bitmap bitmap = BitmapFactory.decodeStream(byStream);
imageView.setImageBitmap(bitmap);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: