微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

E/PDFView:加载 pdf 错误 java.io.FileNotFoundException:打开失败:ENOENT没有那个文件或目录

如何解决E/PDFView:加载 pdf 错误 java.io.FileNotFoundException:打开失败:ENOENT没有那个文件或目录

详细信息:我的应用程序使用以前输入的数据创建一个 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);

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)

请帮忙。我被这个问题困了很长时间。

解决方法

谢谢你的帮助。所以这是解决方案。我替换了这部分代码:

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();

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。