E / PDFCreatorioException:java.io.FileNotFoundException:/storage/emulated/0/PDF/form.pdf(没有这样的文件或目录)

时间:2019-01-10 16:37:15

标签: android

我想创建一个pdf文件。我尝试了几次此代码,并得到了相同的异常。我不知道我做错了什么。请帮助

清单:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

我的代码:

  public void createPdf(String text) {

    Document doc = new Document();

    try {
        String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/PDF";

        File dir = new File(path);
        if(!dir.exists())
            dir.mkdirs();

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

        File file = new File(dir, "form.pdf");
        FileOutputStream fOut = new FileOutputStream(file);

        PdfWriter.getInstance(doc, fOut);
        Paragraph p1 = new Paragraph(text);

        p1.setAlignment(Paragraph.ALIGN_CENTER);

        doc.add(p1);

    } catch (DocumentException de) {
        Log.e("PDFCreator", "DocumentException:" + de);
    } catch (IOException e) {
        Log.e("PDFCreator", "ioException:" + e);
    }
    finally {
        doc.close();
    }
}

0 个答案:

没有答案