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

来源:互联网 发布:禁毒网络知识竞赛 编辑:程序博客网 时间:2024/05/18 02:41

1、将byte[]转换成InputStream  

    byte[] b;

        ByteArrayInputStream bais = new ByteArrayInputStream(b);  
       
  

2、 将InputStream转换成byte[]  

    (1)

InputStream inStream;

    ByteArrayOutputStream swapStream = new ByteArrayOutputStream();

    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) {

  e.printStackTrace();

  }

    byte[] in2b = swapStream.toByteArray();

(2)

    InputStream is;

        String str = ""; 

        byte[] readByte = new byte[1024];  

        int readCount = -1;  

        try {  

            while ((readCount = is.read(readByte, 0, 1024)) != -1) {  

                str += new String(readByte).trim();  

         } catch (Exception e) {  

            e.printStackTrace();  

}  

byte[] in2b = str.getBytes();

  

3、 Bitmap转换成byte[]  

Bitmap bm; 

    ByteArrayOutputStream baos = new ByteArrayOutputStream();  

    bm.compress(Bitmap.CompressFormat.PNG, 100, baos);  

    byte[] b = baos.toByteArray();   

  

4、 byte[]转换成Bitmap  

    byte[] b;

    if (b.length != 0) 

      Bitmap bm = BitmapFactory.decodeByteArray(b, 0, b.length);  

  

5、Drawable转换成Bitmap 

    Drawable drawable; 

    Bitmap bitmap = Bitmap  

            .createBitmap(  

                drawable.getIntrinsicWidth(),  

                drawable.getIntrinsicHeight(),  

                drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888  

                     : Bitmap.Config.RGB_565); 

  

6、 Bitmap转换成Drawable  

    Bitmap bitmap;

    BitmapDrawable bd = new BitmapDrawable(bitmap);  

    Drawable d = (Drawable) bd;  


7、 将Bitmap转换成InputStream  

    Bitmap bm;

    ByteArrayOutputStream baos = new ByteArrayOutputStream(); 

    InputStream is = new ByteArrayInputStream(baos.toByteArray());  

  

8、 将InputStream转换成Bitmap  

    InputStream is;

    Bitmap bm = BitmapFactory.decodeStream(is); 

  

9、 Drawable转换成InputStream  

    Drawable d;

    //先把drawable转换成Bitmap 

    //再把Bitmap 转换成 InputStream    

  

10、 InputStream转换成Drawable  

    InputStream is;

    //先把InputStream 转换成 Bitmap 

    //再把Bitmap 转换成 Drawable  


11、 Drawable转换成byte[] 

    Drawable d;

    //先把Drawable 转换成 Bitmap 

    //再把Bitmap 转换成 byte[]

 

12、 byte[]转换成Drawable  

    byte[] b; 

    //先把byte[] 转换成 Bitmap 

    //再把Bitmap 转换成 Drawable  

  

0 0
原创粉丝点击