您的位置:首页 > 移动开发 > Android开发

Android开发之常用必备工具类图片bitmap转成字符串string与String字符串转换为bitmap图片格式

2017-05-09 21:30 1851 查看
作者:程序员小冰,CSDN博客:http://blog.csdn.net/qq_21376985

QQ986945193 博客园主页:http://www.cnblogs.com/mcxiaobing/

今天给大家提供一个常用的工具类。

Android开发之常用必备工具类图片bitmap转成字符串string与String字符串转换为bitmap图片格式

下面是代码。当然最下面会分享出来源文件。

下面是代码:

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;

import java.io.ByteArrayOutputStream;

/**
* 微博:http://weibo.com/mcxiaobing
* ============================================================================
* Copyright (c) 2015-2016 QQ986945193 All rights reserved.
* ----------------------------------------------------------------------------
* 类名:Android开发之常用必备工具类图片bitmap转成字符串string与String字符串转换为bitmap图片格式
* ----------------------------------------------------------------------------
* 功能描述:Android开发之常用必备工具类图片bitmap转成字符串string与String字符串转换为bitmap图片格式
* ----------------------------------------------------------------------------
*/
public class BitmapAndStringUtils {
/**
* 图片转成string
*
* @param bitmap
* @return
*/
public static String convertIconToString(Bitmap bitmap)
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();// outputstream
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] appicon = baos.toByteArray();<
4000
span class="hljs-comment">// 转为byte数组
return Base64.encodeToString(appicon, Base64.DEFAULT);

}

/**
* string转成bitmap
*
* @param st
*/
public static Bitmap convertStringToIcon(String st)
{
// OutputStream out;
Bitmap bitmap = null;
try
{
// out = new FileOutputStream("/sdcard/aa.jpg");
byte[] bitmapArray;
bitmapArray = Base64.decode(st, Base64.DEFAULT);
bitmap =
BitmapFactory.decodeByteArray(bitmapArray, 0,
bitmapArray.length);
// bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
return bitmap;
}
catch (Exception e)
{
return null;
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
工具类源代码下载地址:http://download.csdn.net/detail/qq_21376985/9591698
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐