详细信息:我的应用程序使用以前输入的数据创建一个 pdf。 PDF 是在应用程序本身中使用 :
创建的//PDF GENERATOR
implementation 'com.itextpdf:itext7-core:7.1.3'
implementation 'com.itextpdf:itextg:5.5.10'
我使用 :
在同一个活动中查看该 PDF//PDF VIEW
implementation 'com.github.barteksc:android-pdf-viewer:2.8.2'
代码如下:
private void createPdf() throws FileNotFoundException {
File folder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "Zomato/Receipts");
file = new File(folder.getAbsolutePath(), "Reciept.pdf");
pdfView.fromFile(file).load();
boolean success = true;
if (!folder.exists()) {
success = folder.mkdirs();
}
OutputStream outputStream = new FileOutputStream(file);
PdfWriter writer = new PdfWriter(file);
PdfDocument pdfDocument = new PdfDocument(writer);
pdfDocument.setDefaultPageSize(PageSize.A4.rotate());
Document document = new Document(pdfDocument);
document.setMargins(15, 15, 15, 15);
float[] columnWidth = {110, 550, 140};
Table table1 = new Table(columnWidth);
//Table1------- 01
table1.addCell(new Cell().add(new Paragraph("Hello Welcome")).setVerticalAlignment(VerticalAlignment.MIDDLE));
table1.addCell(new Cell().add(new Paragraph("This is the PDF Data").setBold().setFontSize(14f)
.setTextAlignment(TextAlignment.CENTER)));
table1.addCell(new Cell().add(new Paragraph("Address : " + Common.currentCompany.getAddress()).setBold().setFontSize(8f).setTextAlignment(TextAlignment.CENTER)));
document.add(table1);
document.close();
textView.setText(R.string.downloaded_message);
}
此代码适用于手机三星 M31,但不适用于 Redmi Y2 和 Oppo。当我在这些手机上运行此代码时,出现以下错误:
E/PDFView: load pdf error
java.io.FileNotFoundException: open failed: ENOENT (No such file or directory)
请帮忙。我被这个问题困了很长时间。
答案 0 :(得分:0)
谢谢你的帮助。所以这是解决方案。我替换了这部分代码:
File folder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "Zomato/Receipts");
file = new File(folder.getAbsolutePath(), "Reciept.pdf");
pdfView.fromFile(file).load();
boolean success = true;
if (!folder.exists()) {
success = folder.mkdirs();
}
OutputStream outputStream = new FileOutputStream(file);
有了这个:
File root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
File folder = new File(root.getAbsolutePath() + "/" + DOCUMENTS);
Log.e("Tag", folder.getAbsolutePath());
if (!folder.exists()) {
folder.mkdirs();
}
File file = new File(folder.getAbsolutePath() + "/" + epoch + ".pdf");
然后我将 PDFView 移到下面的底部:
document.close();
pdfView.fromFile(file).load();