常用的流 bitmap String字节之间的转化


转载:http://blog.csdn.net/bear_huangzhen/article/details/47168123

转载此文章,只是便于自己以后更快的开发,原作者如有什么异议,可联系本人删除即可、


这篇文章是记录我们平时在Android开发过程中,经常会用到的类型转换,主要包括String、byte[]、bitmap、inputstram、Drawable之间的转换,代码如下:


[java]  view plain  copy
  1. <span style="font-size:18px;">import java.io.BufferedReader;  
  2. import java.io.ByteArrayInputStream;  
  3. import java.io.ByteArrayOutputStream;  
  4. import java.io.IOException;  
  5. import java.io.InputStream;  
  6. import java.io.InputStreamReader;  
  7.   
  8. import android.graphics.Bitmap;  
  9. import android.graphics.BitmapFactory;  
  10. import android.graphics.drawable.BitmapDrawable;  
  11. import android.graphics.drawable.Drawable;  
  12.   
  13. public class Demo {  
  14.       
  15.     /** 
  16.      * bitmap 转  byte[]数组 
  17.      */  
  18.     public byte[] bitmap2byteArray(Bitmap bitmap){  
  19.         ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  20.         bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);  
  21.         byte[] bytes = baos.toByteArray();  
  22.         return bytes;  
  23.     }  
  24.       
  25.     /** 
  26.      * bitmap 转  inputstream 
  27.      */  
  28.     public InputStream bitmap2InputStream(Bitmap bitmap){  
  29.         ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  30.         bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);  
  31.         InputStream is = new ByteArrayInputStream(baos.toByteArray());  
  32.         return is;  
  33.     }  
  34.       
  35.     /** 
  36.      * byte[]数组   转   bitmap 
  37.      */  
  38.     public Bitmap byteArray2Bitmap(byte[] bytes){  
  39.         Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);  
  40.         return bitmap;  
  41.     }  
  42.       
  43.     /** 
  44.      * inputstream  转   bitmap  
  45.      */  
  46.     public Bitmap inputStream2Bitmap(InputStream is){  
  47.         Bitmap bitmap = BitmapFactory.decodeStream(is);  
  48.         return bitmap;  
  49.     }  
  50.   
  51.     /** 
  52.      * Drawable 转  bitmap 
  53.      */  
  54.     public Bitmap drawable2Bitmap(Drawable img){  
  55.         BitmapDrawable bd = (BitmapDrawable) img;  
  56.         Bitmap bitmap = bd.getBitmap();  
  57.         return bitmap;  
  58.     }  
  59.       
  60.     /** 
  61.      * bitmap 转  Drawable 
  62.      */  
  63.     public Drawable bitmap2Drawable(Bitmap bitmap){  
  64.         BitmapDrawable bd = new BitmapDrawable(bitmap);  
  65.         Drawable img = bd;  
  66.         return img;  
  67.     }  
  68.       
  69.     /** 
  70.      * String 转  byte[]数组 
  71.      */  
  72.     public byte[] string2ByteArray(String str,String charset){  
  73.         byte[] bytes = null;  
  74.         if(charset == null){  
  75.             bytes = str.getBytes();  
  76.         }else{  
  77.             try {  
  78.                 //如charset = "utf-8"  
  79.                 bytes = str.getBytes(charset);  
  80.             } catch (Exception e) {  
  81.                 // TODO: handle exception  
  82.             }  
  83.               
  84.         }  
  85.         return bytes;  
  86.     }  
  87.       
  88.     /** 
  89.      * String 转  inputstream 
  90.      */  
  91.     public InputStream string2InputStream(String str){  
  92.         InputStream is = new ByteArrayInputStream(str.getBytes());  
  93.         return is;  
  94.     }  
  95.       
  96.     /** 
  97.      * inputstream 转  String  方法01 
  98.      */  
  99.     public String inputStream2String01(InputStream is) throws IOException{  
  100.         ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  101.         int i = -1;  
  102.         while((i=is.read()) != -1){  
  103.             baos.write(i);  
  104.         }  
  105.         return baos.toString();  
  106.     }  
  107.       
  108.     /** 
  109.      * inputstream 转  String   方法02 
  110.      */  
  111.     public String inputStream2String02(InputStream is) throws IOException{  
  112.         BufferedReader br = new BufferedReader(new InputStreamReader(is));  
  113.         StringBuilder sb = new StringBuilder();  
  114.         String line = null;  
  115.         while((line=br.readLine()) != null){  
  116.             sb.append(line+"\n");  
  117.         }  
  118.           
  119.         return sb.toString();  
  120.     }  
  121.       
  122.     /** 
  123.      * inputstream 转  String   方法03 
  124.      */  
  125.     public String inputSteam2String03(InputStream is) throws IOException{  
  126.         StringBuilder sb = new StringBuilder();  
  127.         byte[] b =new byte[1024];  
  128.         for(int n; (n=is.read(b)) != -1;){  
  129.             String s = new String(b, 0, n);  
  130.             sb.append(s);  
  131.         }  
  132.         return sb.toString();  
  133.     }  
  134.       
  135. }</span>  

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值