欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

android Bitmap Drawable bitByte[] 互相转换 Android 

程序员文章站 2022-04-09 08:01:42
...
转换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; 
    } 
}
相关标签: Android