80,351
社区成员
发帖
与我相关
我的任务
分享
ByteArrayOutputStream baos = new ByteArrayOutputStream();
String base64 = null;
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(baos);
oos.writeObject(Bitmap);
base64 = new String(Base64.encode(baos.toByteArray(), Base64.DEFAULT));
} catch (Exception e) {
e.printStackTrace();
}finally{
if (oos != null) {
try {
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
转Bitmap
byte[] objectBytes = Base64.decode(str.getBytes(), Base64.DEFAULT);
ByteArrayInputStream bais = new ByteArrayInputStream(objectBytes);
ObjectInputStream ois = null;
PoiBase poiBase = null;
try {
ois = new ObjectInputStream(bais);
Bitmap = (Bitmap) ois.readObject();
} catch (Exception e) {
e.printStackTrace();
}finally{
if (ois != null) {
try {
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
result = Base64.encodeToString(bitmapBytes, Base64.DEFAULT);
//改成这个试试
result = new String(Base64.encode(bitmapBytes,Base64.NO_WRAP));
/**
* bitmap转为base64
* @param bitmap
* @return
*/
public static String bitmapToBase64(Bitmap bitmap) {
String result = null;
ByteArrayOutputStream baos = null;
try {
if (bitmap != null) {
baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
baos.flush();
baos.close();
byte[] bitmapBytes = baos.toByteArray();
result = Base64.encodeToString(bitmapBytes, Base64.DEFAULT);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (baos != null) {
baos.flush();
baos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}