Drawable drawable = getResources().getDrawable(R.drawable.ic_icon);
BitmapDrawable bitmapDrawable = (BitmapDrawable)drawable;
Bitmap bitmap = bitmapDrawable.getBitmap();
2.Bitmap→Drawable 的简单方法
BitmapDrawable bitmapDrawable = (BitmapDrawable)bitmap;
Drawable drawable = (Drawable)bitmapDrawable;
或者
Drawable drawable = new BitmapDrawable(bitmap);
3、从资源中获取Bitmap
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pic);
4、Bitmap → Byte[]
private byte[] Bitmap2Bytes(Bitmap bitmap){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
}
5、 byte[] → Bitmap
private Bitmap Bytes2Bimap(byte[] bytes){
if(bytes.length!=0){
return BitmapFactory.decodeByteArray(bytes, 0, b.length);
} else{
return null;
}
}