【问题标题】:How to convert base64 to pdf?如何将base64转换为pdf?
【发布时间】:2013-11-13 15:17:47
【问题描述】:

给定一个 base64 输入,我如何在 Android 中将其转换为 PDF 文件?

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = this;
        setContentView(R.layout.activity_main);
        // Get the PDF file to encode it into Base64
        File file = new File("/mnt/sdcard/download/Base64.pdf");
        try {
            // Files is from Guava library
            Files.toByteArray(file);
            // Encoded into Base64
            byte[] temp = Base64.encode(Files.toByteArray(file), Base64.DEFAULT);
            // For testing purposes, wrote the encoded string to file
            writeToFile(Base64.encodeToString(temp, Base64.DEFAULT).toString());

        } catch (IOException e) {
            Log.d(tag, "File.toByteArray() error");
            e.printStackTrace();
        }
    }

我能够使用http://www.webutils.pl/index.php?idx=base64 来测试 PDF 文件是否被正确编码。但我希望自己能够将 base64 解码回 PDF。我该怎么做?

编辑 1

按照 Saneesh CS 的建议尝试了以下代码

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = this;
        setContentView(R.layout.activity_main);
        // Get the PDF file to encode it into Base64
        File file = new File("/mnt/sdcard/download/Base64.pdf");
        try {
            // Files is from Guava library
            Files.toByteArray(file);
            // Encoded into Base64
            byte[] temp = Base64.encode(Files.toByteArray(file), Base64.DEFAULT);
            // Tried with the below line, but same result.
            byte[] temp1 = Base64.encode(Files.toByteArray(file), 0);
            // For testing purposes, wrote the encoded string to file
//          writeToFile(Base64.encodeToString(temp, Base64.DEFAULT).toString());
            final File dwldsPath = new File(Environment.getExternalStorageDirectory(), "test7.pdf");
            byte[] pdfAsBytes = Base64.decode(temp, 0);
            FileOutputStream os;
            os = new FileOutputStream(dwldsPath, false);
            os.write(pdfAsBytes);
            os.flush();
            os.close();
        } catch (IOException e) {
            Log.d(tag, "File.toByteArray() error");
            e.printStackTrace();
        }
    }

以上代码有效。

【问题讨论】:

  • Edit 1 中的代码是编码,而不是解码。
  • 谢谢你,很好。它现在正在工作。

标签: android


【解决方案1】:
final File dwldsPath = new File(DOWNLOADS_FOLDER + fileName + ".pdf");
byte[] pdfAsBytes = Base64.decode(txt, 0);
FileOutputStream os;
os = new FileOutputStream(dwldsPath, false);
os.write(pdfAsBytes);
os.flush();
os.close();

试试这个代码。 txt 是 base64 编码的字符串。

【讨论】:

  • 保存在/storage/emulated/0/Download 更改:DOWNLOADS_FOLDER 为:Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/"
【解决方案2】:

此代码可能会对您有所帮助。我已经执行了附加的代码,它运行良好。

public static GetFilePathAndStatus getFileFromBase64AndSaveInSDCard(String base64, String filename,String extension){
    GetFilePathAndStatus getFilePathAndStatus = new GetFilePathAndStatus();
    try {
        byte[] pdfAsBytes = Base64.decode(base64, 0);
        FileOutputStream os;
        os = new FileOutputStream(getReportPath(filename,extension), false);
        os.write(pdfAsBytes);
        os.flush();
        os.close();
        getFilePathAndStatus.filStatus = true;
        getFilePathAndStatus.filePath = getReportPath(filename,extension);
        return getFilePathAndStatus;
    } catch (IOException e) {
        e.printStackTrace();
        getFilePathAndStatus.filStatus = false;
        getFilePathAndStatus.filePath = getReportPath(filename,extension);
        return getFilePathAndStatus;
    }
}

public static String getReportPath(String filename,String extension) {
    File file = new File(Environment.getExternalStorageDirectory().getPath(), "ParentFolder/Report");
    if (!file.exists()) {
        file.mkdirs();
    }
    String uriSting = (file.getAbsolutePath() + "/" + filename + "."+extension);
    return uriSting;

}
public static class GetFilePathAndStatus{
    public boolean filStatus;
    public String filePath;

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-08
    • 1970-01-01
    • 2020-06-28
    • 1970-01-01
    • 2021-07-25
    • 2021-12-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多