使用ThinkFree的PDF查看器PDF的原始数据显示的Base64恩codeD字符串 [英] Displaying Base64-Encoded String of raw PDF data using ThinkFree PDF Viewer

查看:147
本文介绍了使用ThinkFree的PDF查看器PDF的原始数据显示的Base64恩codeD字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:
我得到的原始的PDF数据的Base64编码恩codeD字符串从Web服务(此数据被安置在我的字符串数组 pdfData )。我一定要使用此数据显示在PDF查看器PDF文档(我碰巧使用附带的ThinkFree的PDF查看器',因为我工作的一个Android应用程序,还是让一概而论说所有PDF阅读器都可以)。请注意,我访问该数组的第0个元素只是为了测试目的(以确保我能写code拉了所有的PDF文件之前至少拉起1 PDF)。

The problem: I'm getting a Base64-Encoded String of raw PDF data from a web service (this data is housed in my String array pdfData). I have to use this data display the PDF in a PDF viewer (I happen to be using the included 'ThinkFree PDF Viewer' since I'm working on an Android application, but lets generalize and say any PDF viewer will do). Note that I'm accessing the 0th element of this array just for testing purposes (to make sure I can at least pull up 1 PDF before writing the code to pull up all the PDFs).

在code是一个类中。首先,方法的CreateFile 被调用,那么 intentOpenPDF

The code is within a class. First the method createFile is called, then 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);
    }
}

错误:
错误打开文件。它不存在或无法读取。

我来检查,如果该文件存在(在 intentOpenPDF 方法内)的条件语句中设置一个断点,它的的传球此检查。

I have a break point set inside the conditional statement that checks if the file exists (within the intentOpenPDF method), and it IS passing this check.

推荐答案

通过调用产生的路径 getFilesDir()导致受保护的目录(文件:///数据/数据/),其中只有openFileOutput(字符串,INT)创建的文件的存储位置。我不是创建文件这种方式。在我的情况的解决方案是使用 getExternalFilesDir(空).getAbsolutePath()相反,它会给你一个路径的内部应用程序的目录(/存储/模拟/ 0 / Android的/数据/)。不需要任何权限来读取或写入到这个路径。

The path produced by calling getFilesDir() leads a protected directory (file:///data/data/), where only files created with openFileOutput(String, int) are stored. I am not creating the file this way. The solution in my case is to use getExternalFilesDir(null).getAbsolutePath() instead, which will give you a path a directory internal to the application (/storage/emulated/0/Android/data/). No permissions are required to read or write to this path.

这篇关于使用ThinkFree的PDF查看器PDF的原始数据显示的Base64恩codeD字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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