Android中Bitmap,byte[],Drawable,InputStream相互转化工具类

本文介绍了一系列数据类型转换的方法,包括byte数组与InputStream、Bitmap、Drawable之间的相互转换,以及Bitmap与byte数组之间的转换等。适用于Android应用开发中不同数据类型间的灵活运用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、将byte[]转换成InputStream  
    public InputStreamByte2InputStream(byte[] b) {  
        ByteArrayInputStream bais = newByteArrayInputStream(b);  
        return bais;  
    }  
   
2、将InputStream转换成byte[]  
两种方式:
一、
 public static final byte[]InputStream2Bytes(InputStream inStream){
   ByteArrayOutputStream swapStream = newByteArrayOutputStream();
    byte[]buff = new byte[100];
    int rc =0;
    try{
  while ((rc =inStream.read(buff, 0, 100)) > 0) {
  swapStream.write(buff, 0,rc);
  }
  } catch (IOException e){
  // TODO Auto-generatedcatch block
 e.printStackTrace();
  }
    byte[]in2b = swapStream.toByteArray();
    returnin2b;
   }
二、
    public byte[]InputStream2Bytes(InputStream is) {  
        String str = "";  
        byte[] readByte = new byte[1024]; 
        int readCount = -1;  
        try {  
            while((readCount = is.read(readByte, 0, 1024)) != -1) { 
                str += newString(readByte).trim();  
           
            returnstr.getBytes();  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        return null;  
    }  
   

3、将Bitmap转换成InputStream  
    public InputStreamBitmap2InputStream(Bitmap bm, int quality) { 
        ByteArrayOutputStream baos = newByteArrayOutputStream();  
        bm.compress(Bitmap.CompressFormat.PNG, quality,baos);  
        InputStream is = newByteArrayInputStream(baos.toByteArray());  
        return is;  
    }  
   
4、将InputStream转换成Bitmap  
    public BitmapInputStream2Bitmap(InputStream is) {  
        return BitmapFactory.decodeStream(is); 
    }  
   
5、Drawable转换成InputStream  
    public InputStreamDrawable2InputStream(Drawable d) {  
        Bitmap bitmap = this.drawable2Bitmap(d); 
        return this.Bitmap2InputStream(bitmap); 
    }  
   
6、InputStream转换成Drawable  
    public DrawableInputStream2Drawable(InputStream is) {  
        Bitmap bitmap = this.InputStream2Bitmap(is); 
        return this.bitmap2Drawable(bitmap); 
    }  
   
7、Drawable转换成byte[]  
    public byte[]Drawable2Bytes(Drawable d) {  
        Bitmap bitmap = this.drawable2Bitmap(d); 
        return this.Bitmap2Bytes(bitmap); 
    }  
8、 byte[]转换成Drawable 
    public DrawableBytes2Drawable(byte[] b) {  
        Bitmap bitmap = this.Bytes2Bitmap(b); 
        return this.bitmap2Drawable(bitmap); 
    }  
   
8、Bitmap转换成byte[]  
    public byte[]Bitmap2Bytes(Bitmap bm) {  
        ByteArrayOutputStream baos = newByteArrayOutputStream();  
        bm.compress(Bitmap.CompressFormat.PNG, 100,baos);  
        return baos.toByteArray(); 
    }  
   
9、byte[]转换成Bitmap  
    public BitmapBytes2Bitmap(byte[] b) {  
        if (b.length != 0) {  
            returnBitmapFactory.decodeByteArray(b, 0, b.length); 
        }  
        return null;  
    }  
   
10、Drawable转换成Bitmap 
    public Bitmapdrawable2Bitmap(Drawable drawable) {  
        Bitmap bitmap = Bitmap  
                .createBitmap( 
                        drawable.getIntrinsicWidth(),  
                        drawable.getIntrinsicHeight(),  
                        drawable.getOpacity() != PixelFormat.OPAQUE ?Bitmap.Config.ARGB_8888  
                                : Bitmap.Config.RGB_565); 
        Canvas canvas = new Canvas(bitmap); 
        drawable.setBounds(0, 0,drawable.getIntrinsicWidth(),  
                drawable.getIntrinsicHeight());  
        drawable.draw(canvas);  
        return bitmap;  
    }  
   
11、Bitmap转换成Drawable  
    public Drawablebitmap2Drawable(Bitmap bitmap) {  
        BitmapDrawable bd = new BitmapDrawable(bitmap); 
        Drawable d = (Drawable) bd; 
        return d;  
   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值