Android中对图像进行Base64编码

 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对象
 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         }

 

posted @ 2014-07-30 13:34  goee  阅读(295)  评论(0编辑  收藏  举报