生成Pdf并保存在Android中的特定文件夹中

时间:2018-08-14 11:20:55

标签: android file pdf

我正在尝试生成PDF并将其保存到特定的文件夹中,并在PDF成功生成后将其打开。但是我遇到了一些例外情况

java.io.FileNotFoundException: /storage/sdcard/PDF/demo.pdf: open failed: ENOENT (No such file or directory)

我也尝试过此solution,但没有成功

我尝试过的代码如下:

 private void cretaePDF() {

        Document doc = new Document();
        try {
            String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/PDF";
            File dir = new File(path);
            if (!dir.exists()) {
                dir.mkdirs();
            }

            Log.d("PDFCreater", "PDF Path" + path);

            File file = new File(dir, "demo.pdf");
            FileOutputStream fout = new FileOutputStream(file);
            PdfWriter.getInstance(doc, fout);
            doc.open();
            Paragraph p1 = new Paragraph("Hi, Its me");
            doc.add(p1);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

要打开PDF,是:

 private void openPDF() {

        Intent intent = new Intent(Intent.ACTION_VIEW);
        String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/PDF";
        File file = new File(path, "demo.pdf");
        intent.setDataAndType(Uri.fromFile(file), "application.pdf");
        startActivity(intent);

    }

什么地方和错误在哪里?

0 个答案:

没有答案