Android Bitmap与byte[]、Bitmap与Drawable相互转化
再很多时候我们需要Bitmap与byte[]、Bitmap与Drawable相互转化,我看网上大多数博客都是复制粘贴的,又乱又不好看,自己写了四个方法记录下来,直接拿过来就能用,方便自己也方便别人。
1、Bitmap与byte[]之间的转化
public static byte[] getBytes(Bitmap bitmap){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 0, baos);
return baos.toByteArray();
}
public static Bitmap getBitmap(byte[] data){
return BitmapFactory.decodeByteArray(data, 0, data.length);
}
2、Bitmap与Drawable之间的转化
public static Bitmap getBitmap(Context context, int drawable){
Drawable mDrawable= ContextCompat.getDrawable(context, drawable);
BitmapDrawable mBitmapDrawable=(BitmapDrawable)mDrawable;
Bitmap mBitmap=mBitmapDrawable.getBitmap();
return mBitmap;
}
public static Drawable getDrawable(Context context,Bitmap bitmap){
Drawable mDrawable1=new BitmapDrawable(context.getResources(),bitmap);
return mDrawable1;
}
拿过来就能用,非常棒!
还没有评论,来说两句吧...