无法在 Android 11(R) 中创建 PDF

Unable to create PDF in Android 11(R)

社区的问候

我正在使用 ITEXT 库生成 PDF 报告。

'com.itextpdf:itextpdf:5.0.6'

我就是这样做的

 Document document = new Document();
   fileName = FileName + Common.getCurrentDateTime() + ".pdf";
   
    fileName1 = FileName + Common.getCurrentDateTime() + ".pdf";
    String dest = context.getExternalFilesDir(null) + "/RTS";

    File dir = new File(dest);
    if (!dir.exists())
        dir.mkdirs();


    try {
        File file = new File(dest, fileName);
        file.createNewFile();
        FileOutputStream fOut = new FileOutputStream(file, false);
        //FileOutputStream fOut = new FileOutputStream(file);
        //PdfWriter.getInstance(document, new FileOutputStream(dest));
        PdfWriter.getInstance(document, fOut);
        //  PdfWriter.getInstance(document, fOut);
    } catch (DocumentException e) {
        e.printStackTrace();
        Log.v("PdfError", e.toString());
        //   dialog.dismiss();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        Log.v("PdfError", e.toString());
        //  dialog.dismiss();


    } catch (IOException e) {
        e.printStackTrace();
    }

请注意,此解决方案在 Android (10)Q 之前工作正常。 我经历了很多解决方案,并花了将近一周的时间来解决这个问题。 我正在模拟器上测试 Android (11)R 场景。

感谢评论此问题的人,现在 PDF 已创建

 document.close();
    //   File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/example.pdf");
    File file = new File(dest+"/"+fileName1);
    // File file = new File("/" +"Internal storage"+ "/" +"RTS" +"/" + fileName);
    if (!file.exists()) {
        file.mkdir();
    }
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    //intent.addCategory(Intent.CATEGORY_OPENABLE);
    intent.setDataAndType(Uri.fromFile(file), "application/pdf");
    intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
    ((Activity) context).startActivity(intent);
    listsOfListReportModelList.clear();

现在无法打开这样的文件。 说要缩短名字。

在Android11

中创建和显示PDF的解决方案

在资源目录中创建@xml/provide_paths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path
        name="external_files"
        path="." />
    <root-path
        name="external_files"
        path="/storage/" />
</paths>

添加这是 Manifest.xml 应用程序标签

     <application
<--other properties-->
            android:requestLegacyExternalStorage="true"
            android:usesCleartextTraffic="true">

     <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="${applicationId}.provider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths" />
        </provider>

现在创建和显示 PDF

    String fileName1 = "";
Document document = new Document();
    // Location to save
    fileName1 = "TEST" + ".pdf";
    String dest = context.getExternalFilesDir(null) + "/";

    File dir = new File(dest);
    if (!dir.exists())
        dir.mkdirs();


    try {
        File file = new File(dest, fileName);
        file.createNewFile();
        FileOutputStream fOut = new FileOutputStream(file, false);
        PdfWriter.getInstance(document, fOut);
    } catch (DocumentException e) {
        e.printStackTrace();
        Log.v("PdfError", e.toString());
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        Log.v("PdfError", e.toString());


    } catch (IOException e) {
        e.printStackTrace();
    }

    // Open to write
        document.open();
        document.add(new Paragraph(""));
        document.add(new Chunk(""));
    } catch (DocumentException e) {
        e.printStackTrace();
    }


      document.close();

    File pdfFile = new File(dest+"/"+fileName1);
    if (!pdfFile.exists()) {
        pdfFile.mkdir();
    }



    if (pdfFile != null && pdfFile.exists() ) //Checking for the file is exist or not
    {
        
        Intent intent = new Intent(Intent.ACTION_VIEW);


        Uri mURI = FileProvider.getUriForFile(
                context,
                context.getApplicationContext()
                        .getPackageName() + ".provider", pdfFile);
        intent.setDataAndType(mURI, "application/pdf");
        intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_GRANT_READ_URI_PERMISSION);

        try {
            context.startActivity(intent);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    } else {

        Toast.makeText(context, "The file not exists! ", Toast.LENGTH_SHORT).show();

    }

这就是你如何通过iText库制作PDF并显示它。