有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

安卓 E/PDFView:加载pdf错误。JAVA木卫一。FileNotFoundException:打开失败:eNot(没有这样的文件或目录)

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

请帮忙。我在这个问题上纠结了很长时间


共 (1) 个答案

  1. # 1 楼答案

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

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