Bitmap Drawable bitByte[] 互相轉換


(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;  
    }  
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章