打印照片
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
拍摄和分享照片是移动设备最受欢迎的用途之一。如果您的应用
拍摄照片、展示照片或允许用户分享图片,则应考虑启用打印功能
和图片。Android 支持库提供了一个便捷的功能,支持使用
只需编写极少的代码和一组简单的打印版式选项。
本节课介绍如何使用 v4 支持库 PrintHelper
类打印图片。
打印图片
Android 支持库 PrintHelper
类提供
一种简单的图片打印方法。该类有一个布局选项 setScaleMode()
,
打印时,您可以选择以下两种方式之一:
setScaleMode()
的这两个缩放选项都能保持图片的现有宽高比不变。以下代码示例
展示了如何创建 PrintHelper
类的实例、设置
缩放选项,然后开始打印过程:
Kotlin
private fun doPhotoPrint() {
activity?.also { context ->
PrintHelper(context).apply {
scaleMode = PrintHelper.SCALE_MODE_FIT
}.also { printHelper ->
val bitmap = BitmapFactory.decodeResource(resources, R.drawable.droids)
printHelper.printBitmap("droids.jpg - test print", bitmap)
}
}
}
Java
private void doPhotoPrint() {
PrintHelper photoPrinter = new PrintHelper(getActivity());
photoPrinter.setScaleMode(PrintHelper.SCALE_MODE_FIT);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.droids);
photoPrinter.printBitmap("droids.jpg - test print", bitmap);
}
此方法可以作为菜单项的操作来调用。请注意,与
并非始终受支持(例如打印)应放置在溢出菜单中。有关
请参阅操作栏设计
指南。
调用 printBitmap()
方法后,
则不需要您的应用执行进一步操作。Android 打印界面
即可允许用户选择打印机和打印选项。然后,用户可以打印
图片或取消操作。如果用户选择打印图片,则系统会创建打印作业并
打印通知。
如果您想在打印输出中添加图片以外的其他内容,则必须
构建打印文档。有关创建打印文档的信息,请参阅
打印 HTML 文档或
打印自定义文档
课程。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-27。
[null,null,["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# Printing photos\n\nTaking and sharing photos is one of the most popular uses for mobile devices. If your application\ntakes photos, displays them, or allows users to share images, you should consider enabling printing\nof those images in your application. The [Android Support Library](/tools/support-library) provides a convenient function for enabling image printing using a\nminimal amount of code and simple set of print layout options.\n\nThis lesson shows you how to print an image using the v4 support library [PrintHelper](/reference/androidx/print/PrintHelper) class.\n\nPrint an image\n--------------\n\nThe Android Support Library [PrintHelper](/reference/androidx/print/PrintHelper) class provides\na simple way to print images. The class has a single layout option, [setScaleMode()](/reference/androidx/print/PrintHelper#setScaleMode(int)),\nwhich allows you to print with one of two options:\n\n- [SCALE_MODE_FIT](/reference/androidx/print/PrintHelper#SCALE_MODE_FIT) - This option sizes the image so that the whole image is shown within the printable area of the page.\n- [SCALE_MODE_FILL](/reference/androidx/print/PrintHelper#SCALE_MODE_FILL) - This option scales the image so that it fills the entire printable area of the page. Choosing this setting means that some portion of the top and bottom, or left and right edges of the image is not printed. This option is the default value if you do not set a scale mode.\n\nBoth scaling options for [setScaleMode()](/reference/androidx/print/PrintHelper#setScaleMode(int)) keep the existing aspect ratio of the image intact. The following code example\nshows how to create an instance of the [PrintHelper](/reference/androidx/print/PrintHelper) class, set the\nscaling option, and start the printing process: \n\n### Kotlin\n\n```kotlin\nprivate fun doPhotoPrint() {\n activity?.also { context -\u003e\n PrintHelper(context).apply {\n scaleMode = PrintHelper.SCALE_MODE_FIT\n }.also { printHelper -\u003e\n val bitmap = BitmapFactory.decodeResource(resources, R.drawable.droids)\n printHelper.printBitmap(\"droids.jpg - test print\", bitmap)\n }\n }\n}\n```\n\n### Java\n\n```java\nprivate void doPhotoPrint() {\n PrintHelper photoPrinter = new PrintHelper(getActivity());\n photoPrinter.setScaleMode(PrintHelper.SCALE_MODE_FIT);\n Bitmap bitmap = BitmapFactory.decodeResource(getResources(),\n R.drawable.droids);\n photoPrinter.printBitmap(\"droids.jpg - test print\", bitmap);\n}\n```\n\n\nThis method can be called as the action for a menu item. Note that menu items for actions that are\nnot always supported (such as printing) should be placed in the overflow menu. For more\ninformation, see the [Action Bar](/design/patterns/actionbar) design\nguide.\n\nAfter the [printBitmap()](/reference/androidx/print/PrintHelper#printBitmap(java.lang.String, android.graphics.Bitmap)) method is\ncalled, no further action from your application is required. The Android print user interface\nappears, allowing the user to select a printer and printing options. The user can then print the\nimage or cancel the action. If the user chooses to print the image, a print job is created and a\nprinting notification appears in the system bar.\n\nIf you want to include additional content in your printouts beyond just an image, you must\nconstruct a print document. For information on creating documents for printing, see the\n[Printing an HTML document](/training/printing/html-docs) or\n[Printing a custom document](/training/printing/custom-docs)\nlessons."]]