图片与数组间的相互转化
在使用room保存图片时,需要转化为数组类型
记录相互转换代码
/**
* bitmap转数组
* @param bm
* @return
*/
public static byte[] Bitmap2Bytes(Bitmap bm) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
}
/**
* 数组转bitmap
* @param b
* @return
*/
public static Bitmap Bytes2Bitmap(byte[] b) {
if (b != null && b.length > 0) {
return BitmapFactory.decodeByteArray(b, 0, b.length);
} else {
return null;
}
}
/**
* 将本地图片转成Bitmap
*
* @param path 已有图片的路径
* @return
*/
public static Bitmap fire2Bitmap(String path) {
Bitmap bitmap = null;
try {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(path));
bitmap = BitmapFactory.decodeStream(bis);
bis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}