当前位置: 代码迷 >> Android >> android Bit地图 Drawable bitByte[] 互相转换
  详细解决方案

android Bit地图 Drawable bitByte[] 互相转换

热度:203   发布时间:2016-05-01 14:23:03.0
android Bitmap Drawable bitByte[] 互相转换
转换Bitmap to Drawable
BitmapDrawable bitmapDrawable = (BitmapDrawable)bitmap;    
Drawable drawable = (Drawable)bitmapDrawable;       
    
Bitmap bitmap = new Bitmap (...);    
Drawable drawable = new BitmapDrawable(bitmap); 

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

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


转换byte[] to Bitmap
private Bitmap Bytes2Bimap(byte[] b){ 
    if(b.length!=0){ 
        return BitmapFactory.decodeByteArray(b, 0, b.length); 
    }else { 
        return null; 
    } 
}
  相关解决方案