0
Printing Images
•
•

Taking and sharing photos is one of the most popular uses for mobile
devices.

•
•
•

If your applica...
Printing Images
•

The Android Support Library PrintHelper class provides a simple way to print of images.

•

The class h...
Printing HTML Documents
•
•

Printing out content beyond a simple photo on Android requires
composing text and graphics in...
Use following code for layout
file(main.xml)
•<?xml version="1.0" encoding="utf-8"?>
•<LinearLayout android:id="@+id/Linea...
Create/Add Activity Name: PrintPdf.java
Use Following Code for PrintPdf.java
Code for PrintPdf.java
import android.app.Act...
Create/Add Activity Name: PrintPdf.java
Use Following Code for PrintPdf.java
PrintPdf.java code continue….
@Override
publi...
Create/Add Activity Name: PrintPdf.java
Use Following Code for PrintPdf.java
PrintPdf.java code continue….

PrintPdf.java ...
Create/Add Activity Name: PrintPdf.java
Use Following Code for PrintPdf.java
PrintPdf.java code continue….
Research prospe...
Manifest File Code
Main Code

New Classes used in the above code:

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns...
App Screen with two options for Image and
HTML Printing and PDF Generation
Save as PDF Dialog Box, Enter
Filename and Hit Save
PDF Files Generated With App
For more details on Printing Content using Android visit following link:
https://developer.an...
Contact Us
Lets Nurture UK

Lets Nurture INDIA

Gumption Business Centre
Glydegate
Bradford
West Yorkshire
BD5 0BQ
United ...
Printing photos-html-using-android
Upcoming SlideShare
Loading in...5
×

Printing photos-html-using-android

2,042

Published on

Published in: Technology, Education
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,042
On Slideshare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
19
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Transcript of "Printing photos-html-using-android"

  1. 1. Printing Images • • Taking and sharing photos is one of the most popular uses for mobile devices. • • • If your application takes photos, displays them, or allows users to share images, you should consider enabling printing of those images in your application. • • • The Android Support Library provides a convenient function for enabling image printing using a minimal amount of code and simple set of print layout options.
  2. 2. Printing Images • The Android Support Library PrintHelper class provides a simple way to print of images. • The class has a single layout option, setScaleMode(), which allows you to print with one of two options: • • •SCALE_MODE_FIT This option sizes the image so that the whole image is shown within the printable area of the page. • • •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. • Both scaling options for setScaleMode() keep the existing aspect ratio of the image intact. The following code example shows how to create an instance of the PrintHelper class, set the scaling option, and start the printing process: •
  3. 3. Printing HTML Documents • • Printing out content beyond a simple photo on Android requires composing text and graphics in a print document. • • The Android framework provides a way to use HTML to compose a document and print it with a minimum of code. • • In Android 4.4 (API level 19), the WebView class has been updated to enable printing HTML content. • • • The class allows you to load a local HTML resource or download a page from the web, create a print job and hand it off to Android's print services. • • Following Code shows you how to quickly build an HTML document containing text and graphics and use WebView to print it.
  4. 4. Use following code for layout file(main.xml) •<?xml version="1.0" encoding="utf-8"?> •<LinearLayout android:id="@+id/LinearLayout01" • android:layout_width="fill_parent" android:layout_height="fill_parent" • xmlns:android="http://schemas.android.com/apk/res/android" • android:orientation="vertical"> •<LinearLayout android:id="@+id/LinearLayout02“ android:layout_width="wrap_content" android:layout_height="wrap_content"> •<Button android:id="@+id/BtnImage" android:layout_width="wrap_content" • android:layout_height="wrap_content" android:text="Image"></Button> •<Button android:id="@+id/BtnHtml" android:layout_width="wrap_content" • android:layout_height="wrap_content" android:text="HTML"></Button> •</LinearLayout> •</LinearLayout>
  5. 5. Create/Add Activity Name: PrintPdf.java Use Following Code for PrintPdf.java Code for PrintPdf.java import android.app.Activity; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.print.PrintAttributes; import android.print.PrintDocumentAdapter; import android.print.PrintJob; import android.print.PrintManager; import android.support.v4.print.PrintHelper; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.Button; PrintPdf.java code continue…. public class PrintPdf extends Activity implements OnClickListener { /** Called when the activity is first created. */ Button Image; Button Html; private WebView mWebView; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Image = (Button) findViewById(R.id.BtnImage); Html = (Button) findViewById(R.id.BtnHtml); Image.setOnClickListener(this); Html.setOnClickListener(this); }
  6. 6. Create/Add Activity Name: PrintPdf.java Use Following Code for PrintPdf.java PrintPdf.java code continue…. @Override public void onClick(View v) { // TODO Auto-generated method stub if (v == Image) { doPhotoPrint(); } if (v == Html) { doWebViewPrint(); } } public class PrintPdf extends Activity implements OnClickListener { /** Called when the activity is first created. */ Button Image; Button Html; private WebView mWebView; /** Called when the activity is first created. */ PrintPdf.java code continue…. @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Image = (Button) findViewById(R.id.BtnImage); Html = (Button) findViewById(R.id.BtnHtml); Image.setOnClickListener(this); Html.setOnClickListener(this); } @Override public void onClick(View v) { // TODO Auto-generated method stub if (v == Image) { doPhotoPrint(); } if (v == Html) { doWebViewPrint(); } }
  7. 7. Create/Add Activity Name: PrintPdf.java Use Following Code for PrintPdf.java PrintPdf.java code continue…. PrintPdf.java code continue…. // Method for Image to PDF private void doPhotoPrint() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { return false; } @Override public void onPageFinished(WebView view, String url) { Log.i("print", "page finished loading " + url); createWebPrintJob(view); mWebView = null; } }); // Generate an HTML document on the fly: String htmlDocument = "<html><body><h1>World's First PDF using Android's PrintManager</h1><p>This is going to be fun for " + "every android developer...</p></body></html>"; PrintHelper photoPrinter = new PrintHelper(this); // PrintHelper photoPrinter = new PrintHelper(this); photoPrinter.setScaleMode(PrintHelper.SCALE_MODE_FIT); Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.icon); photoPrinter.printBitmap("droids.jpg - test print", bitmap); } // Method for HTML -> PDF Conversion private void doWebViewPrint() { // Create a WebView object specifically for printing //WebView webView = new WebView(getActivity()); WebView webView = new WebView(this); webView.setWebViewClient(new WebViewClient() { webView.loadDataWithBaseURL(null, htmlDocument, "text/HTML", "UTF-8", null);
  8. 8. Create/Add Activity Name: PrintPdf.java Use Following Code for PrintPdf.java PrintPdf.java code continue…. Research prospect company. Identify audience. Define presales support (for example, engineers). Plan // Keep a reference to WebView object until you pass the PrintDocumentAdapter to the PrintManager mWebView = webView; } private void createWebPrintJob(WebView webView) { // Get a PrintManager instance PrintManager printManager = (PrintManager)this.getSystemService(Context.PRINT_SERVICE ); // Get a print adapter instance PrintDocumentAdapter printAdapter = webView.createPrintDocumentAdapter(); PrintPdf.java code continue…. // Create a print job with name and adapter instance String jobName = getString(R.string.app_name) + " Document"; PrintJob printJob = printManager.print(jobName, printAdapter, new PrintAttributes.Builder().build()); // Save the job object for later status checking //mPrintJobs.add(printJob); } } meeting agenda. Call and confirm meeting ahead of time.
  9. 9. Manifest File Code Main Code New Classes used in the above code: <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.Viewflipper" android:versionCode="1" android:versionName="1.0"> PrintHelper Helper for printing bitmaps. PrintHelperKitkat Kitkat specific PrintManager API implementation. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-sdk android:minSdkVersion="19" android:targetSdkVersion="19"/> PrintManager System level service for accessing the printing capabilities of the platform. <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".PrintPdf" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> PrintDocumentAdapter Base class that provides the content of a document to be printed. PrintJob This class represents a print job from the perspective of an application.
  10. 10. App Screen with two options for Image and HTML Printing and PDF Generation
  11. 11. Save as PDF Dialog Box, Enter Filename and Hit Save
  12. 12. PDF Files Generated With App For more details on Printing Content using Android visit following link: https://developer.android.com/training/printing/index.html HTML to PDF Image to PDF
  13. 13. Contact Us Lets Nurture UK Lets Nurture INDIA Gumption Business Centre Glydegate Bradford West Yorkshire BD5 0BQ United Kingdom +44(01274)271727 www.letsnurture.co.uk 312 Kalasagar Mall Nr. Satadhar Cross Road Ahmedabad Gujarat 380061 India (+91) 7940095953 www.letsnurture.com
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×