Android中drawable转换为bitmap

2020-11-26  本文已影响0人  Topone

Bitmap转换为Drawable

Drawable drawable =new BitmapDrawable(bmp);

Drawable转换为bitmap

Resources res=getResources();
Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.sample);

从资源文件中获取Bitmap

Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic);

Bitmap转byte[]

private byte[] Bitmap2Bytes(Bitmap bm){
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
    return baos.toByteArray();  
     }

byte[]转Bitmap

private Bitmap Bytes2Bimap(byte[] b){
                    if(b.length!=0){
                            return BitmapFactory.decodeByteArray(b, 0, b.length);
                    }
                    else {
                            return null;
                    }
          }
上一篇下一篇

猜你喜欢

热点阅读