Android中Bitmap,byte[],Drawable,InputStream相互轉化工具類

來源:互聯網
上載者:User

標籤:bitmap

在新浪看到一些資料流的轉化,分享一下:

1、將byte[]轉換成InputStream 

    public InputStream Byte2InputStream(byte[] b) {          ByteArrayInputStream bais = new ByteArrayInputStream(b);          return bais;      }  


2、 將InputStream轉換成byte[]  
兩種方式:
一、
 public static final byte[] InputStream2Bytes(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) {  // TODO Auto-generated catch block  e.printStackTrace();  }    byte[] in2b = swapStream.toByteArray();    return in2b;    }


二、
    
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 += new String(readByte).trim();              }              return str.getBytes();          } catch (Exception e) {              e.printStackTrace();          }          return null;      }  


  
3、 將Bitmap轉換成InputStream  
    
public InputStream Bitmap2InputStream(Bitmap bm, int quality) {          ByteArrayOutputStream baos = new ByteArrayOutputStream();          bm.compress(Bitmap.CompressFormat.PNG, quality, baos);          InputStream is = new ByteArrayInputStream(baos.toByteArray());          return is;      }  


  
4、 將InputStream轉換成Bitmap  
   
 public Bitmap InputStream2Bitmap(InputStream is) {          return BitmapFactory.decodeStream(is);      }  


  
5、 Drawable轉換成InputStream  
 
   public InputStream Drawable2InputStream(Drawable d) {          Bitmap bitmap = this.drawable2Bitmap(d);          return this.Bitmap2InputStream(bitmap);      }  


  
6、 InputStream轉換成Drawable  
   
 public Drawable InputStream2Drawable(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 Drawable Bytes2Drawable(byte[] b) {          Bitmap bitmap = this.Bytes2Bitmap(b);          return this.bitmap2Drawable(bitmap);      }  


  
9、 Bitmap轉換成byte[]  
    public byte[] Bitmap2Bytes(Bitmap bm) {          ByteArrayOutputStream baos = new ByteArrayOutputStream();          bm.compress(Bitmap.CompressFormat.PNG, 100, baos);          return baos.toByteArray();      }  


  
10、 byte[]轉換成Bitmap  
    public Bitmap Bytes2Bitmap(byte[] b) {          if (b.length != 0) {              return BitmapFactory.decodeByteArray(b, 0, b.length);          }          return null;      }  


  
11、Drawable轉換成Bitmap  
    
public Bitmap drawable2Bitmap(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;      }  


  
12、 Bitmap轉換成Drawable  
    public Drawable bitmap2Drawable(Bitmap bitmap) {          BitmapDrawable bd = new BitmapDrawable(bitmap);          Drawable d = (Drawable) bd;          return d;      } 


Android中Bitmap,byte[],Drawable,InputStream相互轉化工具類

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.