话不多说,直接上代码
- /**
- * 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;
- }
- /**
- * base64转为bitmap
- * @param base64Data
- * @return
- */
- public static Bitmap base64ToBitmap(String base64Data) {
- byte[] bytes = Base64.decode(base64Data, Base64.DEFAULT);
- return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
- }
本文提供了一种实现图片(Bitmap)与Base64字符串相互转换的方法。通过给出的两个函数,可以轻松地将Bitmap对象转化为Base64字符串,反之亦然。此技术常用于在网络上传输图片数据。
1007

被折叠的 条评论
为什么被折叠?



