Android中对图像进行Base64编码

来源:互联网 发布:淘宝复制链接在哪打开 编辑:程序博客网 时间:2024/04/28 08:49
Java代码  收藏代码
  1. public String bitmaptoString(Bitmap bitmap) {  
  2.   
  3.   
  4.   
  5.         // 将Bitmap转换成字符串  
  6.   
  7.         String string = null;  
  8.   
  9.         ByteArrayOutputStream bStream = new ByteArrayOutputStream();  
  10.   
  11.         bitmap.compress(CompressFormat.PNG, 100, bStream);  
  12.   
  13.         byte[] bytes = bStream.toByteArray();  
  14.   
  15.         string = Base64.encodeToString(bytes, Base64.DEFAULT);  
  16.   
  17.         return string;  
  18.   
  19. }  

 

    这就是获取位图Base64编码的代码,同理也可以将Base64编码字符串转化为Bitmap对象
    Java代码  收藏代码
    1. public Bitmap stringtoBitmap(String string) {  
    2.   
    3.                 // 将字符串转换成Bitmap类型  
    4.   
    5.                 Bitmap bitmap = null;  
    6.   
    7.                 try {  
    8.   
    9.                         byte[] bitmapArray;  
    10.   
    11.                         bitmapArray = Base64.decode(string, Base64.DEFAULT);  
    12.   
    13.                         bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0,  
    14.   
    15.                                         bitmapArray.length);  
    16.   
    17.                 } catch (Exception e) {  
    18.   
    19.                         e.printStackTrace();  
    20.   
    21.                 }  
    22.   
    23.   
    24.   
    25.                 return bitmap;  
    26.   
    27.         }  
转载自:http://tangweiye.iteye.com/blog/1447519
0 0
原创粉丝点击