Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用ThinkFree PDF Viewer显示原始PDF数据的Base64编码字符串_Java_Android_Pdf_Base64_Fileoutputstream - Fatal编程技术网

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

Java 使用ThinkFree PDF Viewer显示原始PDF数据的Base64编码字符串,java,android,pdf,base64,fileoutputstream,Java,Android,Pdf,Base64,Fileoutputstream,问题是: 我正在从web服务获取原始PDF数据的Base64编码字符串(该数据位于我的字符串数组pdfData)。我必须使用这些数据在PDF查看器中显示PDF(因为我正在使用Android应用程序,所以我碰巧使用了附带的“ThinkFree PDF查看器”,但让我们概括一下,任何PDF查看器都可以)。请注意,我访问此数组的第0个元素只是出于测试目的(确保在编写代码以提取所有PDF之前至少可以提取1个PDF) 代码在一个类中。首先调用方法createFile,然后调用intentOpenPDF:

问题是: 我正在从web服务获取原始PDF数据的Base64编码字符串(该数据位于我的字符串数组
pdfData
)。我必须使用这些数据在PDF查看器中显示PDF(因为我正在使用Android应用程序,所以我碰巧使用了附带的“ThinkFree PDF查看器”,但让我们概括一下,任何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);
    }
}
错误: 打开文件时出错。它不存在或无法读取。


我在条件语句中设置了一个断点,用于检查文件是否存在(在
intentOpenPDF
方法中),并且它正在通过此检查。

考虑到您的错误消息:

它不存在或无法读取。


当您验证它存在时(第一个可能的原因),它肯定是不可读的(第二个可能的原因)。

调用
getFilesDir()
生成的路径引导一个受保护的目录(file:///data/data/),其中仅存储使用openFileOutput(字符串,int)创建的文件。我不是这样创建文件的。本例中的解决方案是使用
getExternalFilesDir(null).getAbsolutePath()
,这将为您提供一个路径,即应用程序内部的目录(/storage/emulated/0/Android/data/)。读取或写入此路径不需要任何权限。

您的文件有哪些权限?该文件可能存在,但无法访问。