如何解码base64字符串并将其转换为pdf / jpg并将其保存在存储中 [英] How to decode base64 string and convert it into pdf/jpg and save it in storage

查看:2778
本文介绍了如何解码base64字符串并将其转换为pdf / jpg并将其保存在存储中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想解码一个base64字符串并将其转换为一个文件(如pdf / jpg)并将其保存到设备中,例如在(/storage/emulated/0/Download/file.pdf)中。

I would like to decode a base64 string and turn it into a file (as pdf/jpg) and save it to device, how for example in (/storage/emulated/0/Download/file.pdf).

对于编码文件我使用此代码:

For encode a file i use this code:

File originalFile = new File("/mnt/sdcard/download/file.pdf");
    String encodedBase64 = null;
    try {
        FileInputStream fileInputStreamReader = new FileInputStream(originalFile);
        byte[] bytes = new byte[(int) originalFile.length()];
        fileInputStreamReader.read(bytes);
        encodedBase64=Base64.encodeToString(bytes,Base64.NO_WRAP);
        messaggio=encodedBase64.toString();
        //encodedBase64 = new String(Base64.encode(bytes));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

现在,我想取消这个base64字符串并将其转换为文件并将其保存在设备上...

Now, i would like to deocde this base64 string and convert it into a file and save it on the device...

有人可以帮助我吗?

谢谢大家=)

推荐答案

你可以试试这个:

FileOutputStream fos = new FileOutputStream(filepath);
fos.write(Base64.decode(base64String, Base64.NO_WRAP));
fos.close();

其中:


  • filepath:新文件的路径

  • base64String:您的要转换的base64字符串

  • filepath: Path to the new file
  • base64String: Your base64 string that you want to convert

这篇关于如何解码base64字符串并将其转换为pdf / jpg并将其保存在存储中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆