使用ThinkFree PDF Viewer显示原始PDF数据的Base64编码的字符串

esme_louise

问题:我从Web服务中获取了原始PDF数据的Base64编码字符串(此数据位于我的String数组中pdfData)。我必须使用此数据在PDF查看器中显示PDF(我正在使用随附的“ ThinkFree PDF Viewer”,因为我正在使用Android应用程序,但是可以概括地说任何PDF查看器都可以)。请注意,我仅出于测试目的而访问此数组的第0个元素(以确保在编写代码以提取所有PDF之前,我至少可以提取1个PDF)。

该代码在一个类中。首先createFile调用该方法,然后intentOpenPDF

import org.apache.commons.codec.binary.Base64;

File file;
FileOutputStream outputStream;
private byte[] decodedContent;
private String[] pdfData;
private String[] pdfFileName;

public void createFile() {

    decodedContent = Base64.decodeBase64(pdfData[0].getBytes());

    try {
        File path = new File(getFilesDir(), "PDFs");
        if (!path.exists()) {
            path.mkdirs();
        }
        file = new File(path, pdfFileName[0]);
        outputStream = new FileOutputStream(file);
        outputStream.write(decodedContent);
        outputStream.close();
    } 
    catch (FileNotFoundException ex) {
        ex.printStackTrace();
    } 
    catch (IOException ex) {
        ex.printStackTrace();
    }
    finally {
        // Make absolutely certain the outputStream is closed
        try {
            if (outputStream != null) {
                outputStream.close();
            }
        }
        catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

public void intentOpenPDF() {

    // Make sure the file exists before accessing it:
    if (file.exists()) {
        Uri targetUri = Uri.fromFile(file);

        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(targetUri, "application/pdf");
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        startActivity(intent);
    }
}

错误: Error opening file. It does not exist or cannot be read.

我条件语句,如果文件存在检查(在里面内破发点集intentOpenPDF法),它IS通过这项检查。

esme_louise

调用getFilesDir()产生的路径导致一个受保护的目录(file:/// data / data /),其中仅存储使用openFileOutput(String,int)创建的文件。我不是用这种方式创建文件的。在我的情况下,解决方案是使用getExternalFilesDir(null).getAbsolutePath(),这将为您提供路径到应用程序内部的目录(/ storage / emulated / 0 / Android / data /)。不需要权限来读取或写入此路径。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用base64编码字符串生成PDF

来自分类Dev

在使用本地文件路径,在线文件路径或数据源作为base64字符串的旁边,是否可以嵌入带HTML的PDF?

来自分类Dev

ngCordova使用emailComposer插件将pdf作为base64字符串附加到电子邮件

来自分类Dev

ngCordova使用emailComposer插件将pdf作为base64字符串附加到电子邮件

来自分类Dev

对于非PDF文件,使用base64字符串发布DocuSign信封失败

来自分类Dev

sapui5 编码 base64 字符串到 pdf

来自分类Dev

从Base64字符串下载PDF

来自分类Dev

pdf.js 使用 base64 渲染为 PDF

来自分类Dev

如何使用 PDF.js 显示 PDF(存储在 base64)中的所有页面?

来自分类Dev

itextSharp数据表转换为pdf base64字符串-pdf损坏

来自分类Dev

PDF.js:在iframe中使用postMessage将base64传递给viewer.html

来自分类Dev

在Ruby on Rails中,如何将Base64编码的字符串设置为pdf附件?

来自分类Dev

Elasticsearch-提取PDF内容并使用base64进行编码

来自分类Dev

PHP base64编码pdf文件

来自分类Dev

如何从 UIWebView 中的 Base64 字符串加载 PDF

来自分类Dev

如何将 PIL 图像数组转换为 base64 格式的 PDF 字符串?

来自分类Dev

如何将Base64编码的二进制文件(使用LZX算法编码)解码回原始字符串

来自分类Dev

PDF文本字符串的编码

来自分类Dev

如何使用base64解码编码的图像字符串

来自分类Dev

iOS使用des ecb解密base64编码的字符串

来自分类Dev

如何使用base64解码编码的图像字符串

来自分类Dev

使用Delphi将字符串变量编码为UTF-16LE base64

来自分类Dev

使用VBScript将Unicode字符串编码为Base64

来自分类Dev

使用RNCryptor解密base64编码和AES加密的字符串

来自分类Dev

如果可以轻松解码,为什么要使用 base64 编码字符串

来自分类Dev

Chrome应用未在iframe中显示Base64编码的PDF

来自分类Dev

无法下载Chrome浏览器中显示的base64编码的pdf

来自分类Dev

Chrome应用未在iframe中显示Base64编码的PDF

来自分类Dev

如何在没有pdf组件的移动浏览器中显示Base64编码的pdf

Related 相关文章

  1. 1

    使用base64编码字符串生成PDF

  2. 2

    在使用本地文件路径,在线文件路径或数据源作为base64字符串的旁边,是否可以嵌入带HTML的PDF?

  3. 3

    ngCordova使用emailComposer插件将pdf作为base64字符串附加到电子邮件

  4. 4

    ngCordova使用emailComposer插件将pdf作为base64字符串附加到电子邮件

  5. 5

    对于非PDF文件,使用base64字符串发布DocuSign信封失败

  6. 6

    sapui5 编码 base64 字符串到 pdf

  7. 7

    从Base64字符串下载PDF

  8. 8

    pdf.js 使用 base64 渲染为 PDF

  9. 9

    如何使用 PDF.js 显示 PDF(存储在 base64)中的所有页面?

  10. 10

    itextSharp数据表转换为pdf base64字符串-pdf损坏

  11. 11

    PDF.js:在iframe中使用postMessage将base64传递给viewer.html

  12. 12

    在Ruby on Rails中,如何将Base64编码的字符串设置为pdf附件?

  13. 13

    Elasticsearch-提取PDF内容并使用base64进行编码

  14. 14

    PHP base64编码pdf文件

  15. 15

    如何从 UIWebView 中的 Base64 字符串加载 PDF

  16. 16

    如何将 PIL 图像数组转换为 base64 格式的 PDF 字符串?

  17. 17

    如何将Base64编码的二进制文件(使用LZX算法编码)解码回原始字符串

  18. 18

    PDF文本字符串的编码

  19. 19

    如何使用base64解码编码的图像字符串

  20. 20

    iOS使用des ecb解密base64编码的字符串

  21. 21

    如何使用base64解码编码的图像字符串

  22. 22

    使用Delphi将字符串变量编码为UTF-16LE base64

  23. 23

    使用VBScript将Unicode字符串编码为Base64

  24. 24

    使用RNCryptor解密base64编码和AES加密的字符串

  25. 25

    如果可以轻松解码,为什么要使用 base64 编码字符串

  26. 26

    Chrome应用未在iframe中显示Base64编码的PDF

  27. 27

    无法下载Chrome浏览器中显示的base64编码的pdf

  28. 28

    Chrome应用未在iframe中显示Base64编码的PDF

  29. 29

    如何在没有pdf组件的移动浏览器中显示Base64编码的pdf

热门标签

归档