Itext7使用FilenotFoundException错误在PDFWriter停止

发布于 2025-01-22 16:26:08 字数 2765 浏览 0 评论 0原文

我试图用ITEXT 7.2.2制作PDF文件,并且它只是在PDFWriter停止工作(IM使用日志以查找其停止的位置),但是应用程序不会崩溃。这是我的代码中的一部分:

public void createPDF(ArrayList<ModelAttendance> arrayListAttendances, Integer attendeeCount){
    try{
        Log.i(TAG, "Create PDF with " +attendeeCount +" Attendee");

        String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Kegiatan/" + eventNama + ".pdf";

        File file = new File(path);
        file.mkdirs();

        PdfWriter writer = new PdfWriter(file);
        PdfDocument pdfDoc = new PdfDocument(writer);
        Document document = new Document(pdfDoc, new PageSize(612, 936).rotate());
        document.setMargins(0,0,0,0);
        ...
    }
}

这个错误:

W/System.err: java.io.FileNotFoundException: /storage/emulated/0/Kegiatan/Kegiatan 2.pdf (Is a directory)
W/System.err:     at java.io.FileOutputStream.open0(Native Method)
W/System.err:     at java.io.FileOutputStream.open(FileOutputStream.java:308)
W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:238)
W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:119)
W/System.err:     at com.itextpdf.commons.utils.FileUtil.getBufferedOutputStream(FileUtil.java:201)
W/System.err:     at com.itextpdf.kernel.pdf.PdfWriter.<init>(PdfWriter.java:138)
W/System.err:     at com.itextpdf.kernel.pdf.PdfWriter.<init>(PdfWriter.java:125)
W/System.err:     at id.go.hulusungaiselatankab.absensikegiatanpemerintahan.HelperClassPDF7.createPDF(HelperClassPDF7.java:42)
W/System.err:     at id.go.hulusungaiselatankab.absensikegiatanpemerintahan.ActivityAttendance.lambda$onCreate$0$id-go-hulusungaiselatankab-absensikegiatanpemerintahan-ActivityAttendance(ActivityAttendance.java:86)
W/System.err:     at id.go.hulusungaiselatankab.absensikegiatanpemerintahan.ActivityAttendance$$ExternalSyntheticLambda0.onClick(Unknown Source:2)
W/System.err:     at android.view.View.performClick(View.java:6597)
W/System.err:     at android.view.View.performClickInternal(View.java:6574)
W/System.err:     at android.view.View.access$3100(View.java:778)
W/System.err:     at android.view.View$PerformClick.run(View.java:25885)
W/System.err:     at android.os.Handler.handleCallback(Handler.java:873)
W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:99)
W/System.err:     at android.os.Looper.loop(Looper.java:193)
W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6669)
W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
W/System.err:     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

I tried to make PDF file with Itext 7.2.2 and it just stop working at PdfWriter (im using Log to find where it stop) but the application doesnt crash. Here is my part of the code:

public void createPDF(ArrayList<ModelAttendance> arrayListAttendances, Integer attendeeCount){
    try{
        Log.i(TAG, "Create PDF with " +attendeeCount +" Attendee");

        String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Kegiatan/" + eventNama + ".pdf";

        File file = new File(path);
        file.mkdirs();

        PdfWriter writer = new PdfWriter(file);
        PdfDocument pdfDoc = new PdfDocument(writer);
        Document document = new Document(pdfDoc, new PageSize(612, 936).rotate());
        document.setMargins(0,0,0,0);
        ...
    }
}

This the error:

W/System.err: java.io.FileNotFoundException: /storage/emulated/0/Kegiatan/Kegiatan 2.pdf (Is a directory)
W/System.err:     at java.io.FileOutputStream.open0(Native Method)
W/System.err:     at java.io.FileOutputStream.open(FileOutputStream.java:308)
W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:238)
W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:119)
W/System.err:     at com.itextpdf.commons.utils.FileUtil.getBufferedOutputStream(FileUtil.java:201)
W/System.err:     at com.itextpdf.kernel.pdf.PdfWriter.<init>(PdfWriter.java:138)
W/System.err:     at com.itextpdf.kernel.pdf.PdfWriter.<init>(PdfWriter.java:125)
W/System.err:     at id.go.hulusungaiselatankab.absensikegiatanpemerintahan.HelperClassPDF7.createPDF(HelperClassPDF7.java:42)
W/System.err:     at id.go.hulusungaiselatankab.absensikegiatanpemerintahan.ActivityAttendance.lambda$onCreate$0$id-go-hulusungaiselatankab-absensikegiatanpemerintahan-ActivityAttendance(ActivityAttendance.java:86)
W/System.err:     at id.go.hulusungaiselatankab.absensikegiatanpemerintahan.ActivityAttendance$ExternalSyntheticLambda0.onClick(Unknown Source:2)
W/System.err:     at android.view.View.performClick(View.java:6597)
W/System.err:     at android.view.View.performClickInternal(View.java:6574)
W/System.err:     at android.view.View.access$3100(View.java:778)
W/System.err:     at android.view.View$PerformClick.run(View.java:25885)
W/System.err:     at android.os.Handler.handleCallback(Handler.java:873)
W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:99)
W/System.err:     at android.os.Looper.loop(Looper.java:193)
W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6669)
W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
W/System.err:     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心安伴我暖 2025-01-29 16:26:08

我将代码从以下方式更改为

  File file = new File(path);
  file.mkdirs();

  File root = new File(path);
  if (!root.exists()){
       root.mkdirs();
  }

  File file = new File(path  + eventNama + ".pdf");
  if(root.exists()){
       file.delete();
  }
  file.createNewFile();

  FileOutputStream outputStream = new FileOutputStream(file);

我从这里得到答案:如何在Android应用程序中创建文件?

I change my code from:

  File file = new File(path);
  file.mkdirs();

To:

  File root = new File(path);
  if (!root.exists()){
       root.mkdirs();
  }

  File file = new File(path  + eventNama + ".pdf");
  if(root.exists()){
       file.delete();
  }
  file.createNewFile();

  FileOutputStream outputStream = new FileOutputStream(file);

I got the answer from here: How to create files to a specific folder in android application?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文