Base64字符串与文件相互转换

一.base64转文件

1.1基础封装方法

 * @description  base64字符串转文件
     * @param base64  base64字符串
     * @param name  创建的文件名
     * @return base64转换后的文件
     **/
    public static File base64ToFile(String base64, String name) {
        String fname = name + DirKeys.DIR_POSTFIX_PNG;//保存照片的文件名:名字+格式,xxx.png
        FileUtils.mkdirs(DirKeys.DIR_PICTURE);//创建文件目录
        File picture = new File(DirKeys.DIR_PICTURE, fname);//保存照片的文件
        FileOutputStream out = null;
        try {
            // 解码,然后将字节转换为文件
            byte[] bytes = Base64.decode(base64, Base64.DEFAULT);// 将字符串转换为byte数组
            ByteArrayInputStream in = new ByteArrayInputStream(bytes);
            byte[] buffer = new byte[1024];
            out = new FileOutputStream(picture);
            int byteread;
            while ((byteread = in.read(buffer)) != -1) {
                out.write(buffer, 0, byteread); // 文件写操作
            }
        } catch (IOException ioe) {
            Logs.e("Base64File38:" + ioe);
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
                Logs.e("Base64File46:" + e);
            }
        }
        return picture;
    }

2.2添加监听事件

如果多条base64字符串需要同时转文件,需要设置监听事件,让字符串和文件一一对应,不然会混乱。

Base64和File相互转换所需要的接口回调
public interface Base64FileListener {
    void ResponseString(String ResponseString);//返回的错误原因字符串

    void ResponseFile(File ResponseFile);//返回的文件
}

2.3升级后的方法

    public static void base64ToFile(String base64, String name, Base64FileListener listener) {
        String fname = name + DirKeys.DIR_POSTFIX_PNG;//保存照片的名字
        FileUtils.mkdirs(DirKeys.DIR_PICTURE);
        File picture = new File(DirKeys.DIR_PICTURE, fname);//保存照片的文件
        FileOutputStream out = null;
        try {
            // 解码,然后将字节转换为文件
            byte[] bytes = Base64.decode(base64, Base64.DEFAULT);// 将字符串转换为byte数组
            ByteArrayInputStream in = new ByteArrayInputStream(bytes);
            byte[] buffer = new byte[1024];
            out = new FileOutputStream(picture);
            int byteread;
            while ((byteread = in.read(buffer)) != -1) {
                out.write(buffer, 0, byteread); // 文件写操作
            }
        } catch (IOException e) {
            Logs.e("Base64FileUtil110:" + e);
            listener.ResponseString(e.toString());
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                Logs.e("Base64FileUtil120:" + e);
                listener.ResponseString(e.toString());
            }
        }
        listener.ResponseFile(picture);
    }

2.4方法调用

    /*Base64字符串转文字测试*/
    private void test() {
        Base64FileUtil.base64ToFile(Constant.image0, "image0", new Base64FileListener() {
            @Override
            public void ResponseString(String ResponseString) {
                Logs.e("image0转图片失败,原因:"+ResponseString);
            }

            @Override
            public void ResponseFile(File ResponseFile) {
                image0 = ResponseFile.getAbsolutePath();
                Logs.i("image0图片地址:" + image0);
            }
        });
    }

二.文件转base64

file:需要转换的文件

FileInputStream fileInputStream = new FileInputStream(file);
byte[] bytes = new byte[fileInputStream.available()];
fileInputStream.read(bytes);
/*需要上传的图片信息(为照片base64照片编码)*/
picBase64 = Base64.encodeToString(bytes, Base64.DEFAULT);
Logs.v("为照片base64照片编码:\n" + picBase64);

2.1封装成方法

    /**
     * 文件转base64字符串
     *
     * @param file 需要转换的文件
     * @return 转换成功后的字符串
     */
    public static String fileToBase64(File file) {
        String base64 = null;
        InputStream in = null;
        try {
            in = new FileInputStream(file);
            byte[] bytes = new byte[in.available()];
            int length = in.read(bytes);
            base64 = Base64.encodeToString(bytes, 0, length, Base64.DEFAULT);
        } catch (FileNotFoundException e) {
            Logs.e("Base64FileUtil 77:" + e);
        } catch (IOException e) {
            Logs.e("Base64FileUtil 79:" + e);
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (IOException e) {
                Logs.e("Base64FileUtil 86:" + e);
            }
        }
        return base64;
    }

三.注意事项

Base64转文件,无需考虑文件的格式,base64只是一种数据模式,最终是转成字节数据,字节数据再转成你需要的格式。

特殊字符的替换

base64有符号格式的限值,如果有特殊符号需要如下方式替换

replace("_", "/").replace("-","+")

四.glide加载base64

Glide.with(this).load(Base64.decode("base64字符串",Base64.DEFAULT)).into(photoView);

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值