Bitmap Drawable bitByte[] 互相转换

来源:互联网 发布:ubuntu设置开机自启 编辑:程序博客网 时间:2024/05/21 17:13


(1)转换Bitmap to Drawable 
BitmapDrawable bitmapDrawable = (BitmapDrawable)bitmap;     
Drawable drawable = (Drawable)bitmapDrawable;        
     
Bitmap bitmap = new Bitmap (...);     
Drawable drawable = new BitmapDrawable(bitmap);  

(2)转换Drawable to Bitmap 
Drawable d = ImagesList.get(0);  
Bitmap bitmap = ((BitmapDrawable)d).getBitmap();  

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

(4)转换byte[] to Bitmap 
private Bitmap Bytes2Bimap(byte[] b){  
    if(b.length!=0){  
        return BitmapFactory.decodeByteArray(b, 0, b.length);  
    }else {  
        return null;  
    }  
}
原创粉丝点击