Android开发之Base64与bitmap互转的工具类

老套路先看效果图:

如上图:分别是bitmap转base64和base64转bitmap

直接上代码。看工具类

package com.example.apidemo.util;
/*
 * Copyright (c) 2019, [email protected] All Rights Reserved.
 * #                                                   #
 * #                       _oo0oo_                     #
 * #                      o8888888o                    #
 * #                      88" . "88                    #
 * #                      (| -_- |)                    #
 * #                      0\  =  /0                    #
 * #                    ___/`---'\___                  #
 * #                  .' \\|     |# '.                 #
 * #                 / \\|||  :  |||# \                #
 * #                / _||||| -:- |||||- \              #
 * #               |   | \\\  -  #/ |   |              #
 * #               | \_|  ''\---/''  |_/ |             #
 * #               \  .-\__  '-'  ___/-. /             #
 * #             ___'. .'  /--.--\  `. .'___           #
 * #          ."" '<  `.___\_<|>_/___.' >' "".         #
 * #         | | :  `- \`.;`\ _ /`;.`/ - ` : | |       #
 * #         \  \ `_.   \_ __\ /__ _/   .-` /  /       #
 * #     =====`-.____`.___ \_____/___.-`___.-'=====    #
 * #                       `=---='                     #
 * #     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~   #
 * #                                                   #
 * #               佛祖保佑         永无BUG              #
 * #                                                   #
 */

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

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author 下一页5(轻飞扬)
 * 创建时间:2019/11/12 13:48
 * 个人小站:http://yhsh.wap.ai(已挂)
 * 最新小站:http://www.iyhsh.icoc.in
 * 联系作者:企鹅 13343401268
 * 博客地址:http://blog.csdn.net/xiayiye5
 * 空间名称:ApiDemo
 * 项目包名:com.example.apidemo
 */
public class Base64Utils {
    /**
     * 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);
    }
}

接下来看下调用方法:

 public void imageToBase64(View view) {
        BitmapDrawable drawable = (BitmapDrawable) ivImage1.getDrawable();
        Bitmap bitmap = drawable.getBitmap();
        String s = Base64Utils.bitmapToBase64(bitmap);
        tvBase64Data1.setText(s);
    }

    public void base64ToImage(View view) {
        Bitmap bitmap = Base64Utils.base64ToBitmap(tvBase64Data1.getText().toString());
        ivImage2.setImageBitmap(bitmap);
        try {
            InputStream open = getAssets().open("flower2.jpg");
            Bitmap image = BitmapFactory.decodeStream(open);
            ivImage3.setImageBitmap(image);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
发布了191 篇原创文章 · 获赞 105 · 访问量 36万+

猜你喜欢

转载自blog.csdn.net/xiayiye5/article/details/103031199