Itext7使用FilenotFoundException错误在PDFWriter停止
我试图用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我将代码从以下方式更改为
:
我从这里得到答案:如何在Android应用程序中创建文件?
I change my code from:
To:
I got the answer from here: How to create files to a specific folder in android application?