向社区致以问候
我正在使用 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();
现在无法打开这样的文件。 说要把名字改短一点。
最佳答案
在 Android 11 中创建和显示 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 并显示它。
关于android - 无法在 Android 11(R) 中创建 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66260807/