1page.title=Printing Photos 2parent.title=Printing Content 3parent.link=index.html 4 5trainingnavtop=true 6next.title=Printing HTML Documents 7next.link=html-docs.html 8 9@jd:body 10 11<div id="tb-wrapper"> 12<div id="tb"> 13 14<h2>This lesson teaches you to</h2> 15<ol> 16 <li><a href="#image">Print an Image</a></li> 17</ol> 18 19</div> 20</div> 21 22<p> 23 Taking and sharing photos is one of the most popular uses for mobile devices. If your application 24 takes photos, displays them, or allows users to share images, you should consider enabling printing 25 of those images in your application. The <a href="{@docRoot}tools/support-library/index.html" 26 >Android Support Library</a> provides a convenient function for enabling image printing using a 27 minimal amount of code and simple set of print layout options. 28</p> 29 30<p>This lesson shows you how to print an image using the v4 support library {@link 31 android.support.v4.print.PrintHelper} class.</p> 32 33 34<h2 id="image">Print an Image</h2> 35 36<p>The Android Support Library {@link android.support.v4.print.PrintHelper} class provides 37 a simple way to print of images. The class has a single layout option, {@link 38 android.support.v4.print.PrintHelper#setScaleMode setScaleMode()}, which allows you to print with 39 one of two options:</p> 40 41<ul> 42 <li>{@link android.support.v4.print.PrintHelper#SCALE_MODE_FIT SCALE_MODE_FIT} - This 43 option sizes the image so that the whole image is shown within the printable area of the page. 44 </li> 45 <li>{@link android.support.v4.print.PrintHelper#SCALE_MODE_FILL SCALE_MODE_FILL} - This 46 option scales the image so that it fills the entire printable area of the page. Choosing this 47 setting means that some portion of the top and bottom, or left and right edges of the image is 48 not printed. This option is the default value if you do not set a scale mode.</li> 49</ul> 50 51<p>Both scaling options for {@link android.support.v4.print.PrintHelper#setScaleMode 52 setScaleMode()} keep the existing aspect ratio of the image intact. The following code example 53 shows how to create an instance of the {@link android.support.v4.print.PrintHelper} class, set the 54 scaling option, and start the printing process:</p> 55 56<pre> 57private void doPhotoPrint() { 58 PrintHelper photoPrinter = new PrintHelper(getActivity()); 59 photoPrinter.setScaleMode(PrintHelper.SCALE_MODE_FIT); 60 Bitmap bitmap = BitmapFactory.decodeResource(getResources(), 61 R.drawable.droids); 62 photoPrinter.printBitmap("droids.jpg - test print", bitmap); 63} 64</pre> 65 66<p> 67 This method can be called as the action for a menu item. Note that menu items for actions that are 68 not always supported (such as printing) should be placed in the overflow menu. For more 69 information, see the <a href="{@docRoot}design/patterns/actionbar.html">Action Bar</a> design 70 guide. 71</p> 72 73<p>After the {@link android.support.v4.print.PrintHelper#printBitmap printBitmap()} method is 74 called, no further action from your application is required. The Android print user interface 75 appears, allowing the user to select a printer and printing options. The user can then print the 76 image or cancel the action. If the user chooses to print the image, a print job is created and a 77 printing notification appears in the system bar.</p> 78 79<p>If you want to include additional content in your printouts beyond just an image, you must 80 construct a print document. For information on creating documents for printing, see the 81 <a href="html-docs.html">Printing an HTML Document</a> or 82 <a href="custom-docs.html">Printing a Custom Document</a> 83 lessons.</p> 84 85