3

我正在 Eclipse 中开发一个 android 应用程序以通过 Google Cloud Print 打印报告。谷歌的这个教程让我走上了正确的道路。
我已经安装了itext来生成 Report.pdf

我的问题出在我的应用程序 UI 的“打印”元素中。谷歌提供此代码:

Intent printIntent = new Intent(this, PrintDialogActivity.class);
printIntent.setDataAndType(docUri, docMimeType);
printIntent.putExtra("title", docTitle);
startActivity(printIntent);

随着消息:

"在上面的代码中,将三个参数替换为: docUri - 待打印文档的URI docMimeType - 待打印文档的MIME类型。我们建议您使用PDF(application/pdf)格式 docTitle - 文件的标题打印文档,将在 GCP 管理控制台上显示为打印作业标题的任意字符串

我的 PDF 保存在/data/Student/StudentReportPDF.pdf

我尝试更改 docUri、docMimeType 和 docTitle 如下: 没有运气

Intent printIntent = new Intent(this, PrintDialogActivity.class);
printIntent.setDataAndType(StudentReportPDF, .pdf);
printIntent.putExtra("title", Student Report);
startActivity(printIntent);

所有 3 个元素都会产生红色波浪线。

有人可以发现我的错误,或者我想为我澄清这三个要素吗?

打印 eclipse-plugin pdf-generation itext google-cloud-print

4

1 回答 1

1

非常感谢来自 Reddit 的 DragonRancher

dragonrancher 1 点 1 小时前 使用 Java 语言时,需要将字符串放在双引号之间,否则将被解释为标识符。您的 putExtra 行应该如下所示:

printIntent.putExtra("title", "Student Report");

但是 setDataAndType 行还有其他问题。根据文档, setDataAndType 方法需要一个 Uri 对象和一个包含 MIME 类型的字符串。在这种情况下,您可以使用 Uri.fromFile 或 Uri.parse 方法从文件名中获取 Uri 对象,并且 PDF 的 MIME 类型是 application/pdf:

printIntent.setDataAndType(Uri.fromFile(new File("Report.pdf")), "application.pdf");

我实际上对 Android 没有任何经验,因此其他人可能能够更好地评论确定报告文件的路径。

于 2012-02-28T20:00:53.867 回答