Android 将图片转换为Base64
public void convertToBase64(View view) throws IOException {
//获取ImageView的图片
BitmapDrawable drawable = (BitmapDrawable) mPetIV.getDrawable();
Bitmap bitmap = drawable.getBitmap();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
//以没有压缩的方式讲图片写入到内流中
bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
//将保存在内存中的图片转换为base64
String base64 = Base64.encodeToString(byteArrayOutputStream.toByteArray(), Base64.DEFAULT);
L.e(base64);
byteArrayOutputStream.close();
}
Base64 转化为图片
/**
* 将Base64 转换为图片
*/
public void fromBase64(View view) {
byte[] buffer = Base64.decode(mBase64, Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(buffer, 0, buffer.length);
mPetIV.setImageBitmap(bitmap);
}