项目中涉及到本地图片需要上传到服务器,为了节省服务器存储,将图片以Base64的形式存储,此时就需要将选择的图片转换成Base64的字符串结果上传到服务器
* 将图片转成byte数组
* @param bitmap 图片
* @return 图片的字节数组
public
static
byte
[
]
bitmap2Byte
(
Bitmap
bitmap
)
{
if
(
null
==
bitmap
)
throw
new
NullPointerException
(
)
;
ByteArrayOutputStream
outputStream
=
new
ByteArrayOutputStream
(
)
;
bitmap
.
compress
(
Bitmap
.
CompressFormat
.
JPEG
,
100
,
outputStream
)
;
return
outputStream
.
toByteArray
(
)
;
* 将图片转成byte数组
* @param imageByte 图片
* @return Base64 String
public
static
String
byte2Base64
(
byte
[
]
imageByte
)
{
if
(
null
==
imageByte
)
return
null
;
return
Base64
.
encodeToString
(
imageByte
,
Base64
.
DEFAULT
)
;
项目中涉及到本地图片需要上传到服务器,为了节省服务器存储,将图片以Base64的形式存储,当需要显示的时候若服务端返回的是Base64格式的图片,此时需要将Base64的字符串转换成图片进行展示给用户查看
data
:
image
/
png
;
base64
,
iVBORw0KGgoAAAANSUhEUgAAADwAAAA8CAYAAAA6
/
NlyAAADuklEQVRoQ
+
2
aW
+
hNWRzHP
/
+
US5FEeZIkcnlR1FAKCUmJkQyN3EZTcsmtyJ1IEQYjNLkkl0gSTzNkFCFeRhLKpMkb8uBBU4q
++
a1ajrP
/
Z
+
991l
n7
+
Nur9svZa
/
1
+
38
/
6
rb3W77f3aeE7ay3fGS8lcFuPeBnhMsJtbAbKJd3GAvoVThnhMsLhZ6A7sNjMHgJehXeRbDHmkm4HLDHYvibpBfA7sA
/
4
EAM8FvBcAx2aAPXIwA83GrrRwFMMdGxKkLuAlvmplP0zd2sU8Ghbvj9mVvR5wHXgAHA55
/
jEYaGBhwBLgXmBhAp4F3A7kL1g5WEfYJldobT5ds4A24An9RqvN8LdgOXAhnqFpBx
/
FFhfz1GWF1jjVtisd0opNlS3
/
4E9
wDrgY1ajeYAXGmjPrM4C938D7DD41KazAM8w0H6prcfp
+
B
+
wETiZxl0a4IkGmpQ0pPETo88
/
9
nxfbc1ZLWClfYtiqA3o40Rrx2ItYKV8gwOKiWFK
+
bmOyaqtFrBSwu3ADzGUBvDxEFgN
/
JkX2I2bZTti7wCiGmliDPB3Pc
+
wP1blnc7erUDHRqrOaPsc8JONCQrsdOj5ELgr4jPqC9b9CrAJ6ArcaCSwUzzKwCcHQ0hnSKBKMd3xo8osCrCTp
+
WkwmF4Or25e6lW
/
g3QEvZbdGDnXGWhLvf6JjdZxcDnwH67qtmMCqxnaIunoodXJnapk
/
idRVRRfe3ZqvQZHVjn3hzgoidqkEX715zQRyyij73xC4CDwM6KSY4OvNlE3bOd
+
4
EnUhubnu
+
pKcEvWVRvev3H228D7Df581dVYcBO4x9WbKiKcW26RXxkAvgti
+
gF7
/
5
AYC8woWJM0wE7fWsM4r0nWIWIIt7ffntm0dObStc6A7uBpMehaYEF8NKSg2MekF4LaTdX0
+
771
ru31s52bX5JramBnehrBqekoVqbCawE0tTc3wSwgzxrS1gbnNoIA53WSkQrb31TwE78U
/
uWlKfWLhRY34yOZ4hOiK4
/
A6c9Q1GPJfn9BZhvyzMEUJKNO4A2Px17fosO7Jxr49EnlnGBqf
+
yVaTnv1orDNiJmQQoDUybXSXNj8o
/
lYFJO7sbVziwEzLMztvZGSOu9FJfDl2NW2t40wA7ob2AVZY5dWhF
/
XnLsO7XIqxyX5un3rf5OXZVM7XeWubwnTikvYHr9ZD
+
5
+
Gadlx9MvEro5B
+
v7AVE9h3rF1dkdeX
/
n8bRlfFcFHAMRmbIsIlcKwZKJd0rJkuyk8Z4aJmPpbfMsKxZrooP2WEi5r5WH4
/
Ab28sT3nmvuDAAAAAElFTkSuQmCC
* Base64转Bitmap
* @param base64 base64数据流
* @return Bitmap 图片
private
static
Bitmap
base642Bitmap
(
String
base64String
)
{
if
(
null
==
base64String
)
throw
new
NullPointerException
(
)
;
byte
[
]
decode
=
Base64
.
decode
(
base64String
.
split
(
","
)
[
1
]
,
Base64
.
DEFAULT
)
;
Bitmap
mBitmap
=
BitmapFactory
.
decodeByteArray
(
decode
,
0
,
decode
.
length
)
;
return
mBitmap
;
String
base64
=
"data:image/png;base64,iVBORw0KGgoAA"
;
imageView
.
setImageBitmap
(
base642Bitmap
(
base64
)
)
;
此处注意:
data:image/png;base64, 返回的头编码,若服务器返回带此data:image/*;base64, 类型的头编码时需要在decode的时候将头编码截取掉再进行转换 ,否则将无法还原成功.
ps: data:image/png 此处的png格式是上传时的图片格式 即 data:image/* “*” 代表的是上传是的图片格式(后缀)
base64String.split(",")[1]
在线转换工具
private static String bitmapToBase64(Bitmap bitmap) {
String result = null;
ByteArrayOutputStream baos = null;
try {
if (bitmap != null) {
public static String bitmapToBase64(Bitmap bitmap) {
String result = null;
ByteArrayOutputStream baos = null;
try {
if (bitmap != null) {
baos = new ByteArrayOutputStream();
bitmap.comp.
@(CSDN)[Android]
Convert base64 image data to an image file(.png) and save it to Local file system
String base64Img = "";
if (base64Img.contains(",")) {
将pdf文件转换成图片并显示在界面,
先要引用AndroidPDFViewerLibrary-master
ByteBuffer buffer = ByteBuffer.NEW(bytes);
String data = Base64.encodeToString(bytes, Base64.DEFAULT);
PDFFile pdf_file = new PDFFile(buffer);
int numPages = pdf_file.getNumPages();
Log.v("tag","numPages:"+numPages);
PDFPage page = pdf_file.getPage(0,true);
float scale=2f;
Bitmap image = page.getImage((int)(page.getWidth() * scale), (int)(page.getHeight() * scale), null, true, true);