Bitmap转换成byte[]

// Launcher 4.0 中Bitmap转换成byte[]数组的方法
static byte[] flattenBitmap(Bitmap bitmap) {
        // Try go guesstimate how much space the icon will take when serialized
        // to avoid unnecessary allocations/copies during the write.
        int size = bitmap.getWidth() * bitmap.getHeight() * 4; //每个像素占32bit,所以*4
        ByteArrayOutputStream out = new ByteArrayOutputStream(size);
        try {
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
            out.flush();
            out.close();
            return out.toByteArray();
        } catch (IOException e) {
            Log.w("Favorite", "Could not write icon");
            return null;

        }

}
// 从byte[] 创建bitmap
public static Bitmap bytes2Bitmp(byte[] icon) {
        Bitmap bmp = BitmapFactory.decodeByteArray(icon, 0, icon.length);
        return bmp;
}


你可能感兴趣的:(Bitmap转换成byte[])