1、将图片转成Bitmap
- Resources
res = getResources(); - Bitmap
bmp = BitmapFactory.decodeResource(res, R.drawable.icon);
2、Bitmap 转 byte[ ]
- public
byte[] Bitmap2Bytes(Bitmap bm) { -
ByteArrayOutputStream baos = new ByteArrayOutputStream(); -
bm.compress(Bitmap.CompressFormat.PNG, 100, baos); -
return baos.toByteArray(); - }
- public
Bitmap Bytes2Bimap(byte[] b) { -
if (b.length != 0) { -
return BitmapFactory.decodeByteArray(b, 0, b.length); -
} else { -
return null; -
} - }
- public
static Bitmap drawableToBitmap(Drawable drawable) { -
// 取 drawable 的长宽 -
int w = drawable.getIntrinsicWidth(); -
int h = drawable.getIntrinsicHeight(); -
-
// 取 drawable 的颜色格式 -
Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 -
: Bitmap.Config.RGB_565; -
// 建立对应 bitmap -
Bitmap bitmap = Bitmap.createBitmap(w, h, config); -
// 建立对应 bitmap 的画布 -
Canvas canvas = new Canvas(bitmap); -
drawable.setBounds(0, 0, w, h); -
// 把 drawable 内容画到画布中 -
drawable.draw(canvas); -
return bitmap; -
}
- Bitmap
bm=xxx; //xxx根据你的情况获取 - BitmapDrawable
bd= new BitmapDrawable(getResource(), bm);