printing photos-html-using-android

14
Printing Photos & HTML using Android 4.4 API (Version 19) Presented by [www.letsnurture.com]

Upload: ketan-raval

Post on 09-May-2015

1.888 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Printing photos-html-using-android

Printing Photos & HTML using Android 4.4 API (Version 19)

Presented by [www.letsnurture.com]

Page 2: Printing photos-html-using-android

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.

Page 3: Printing photos-html-using-android

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:

Page 4: Printing photos-html-using-android

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.

Page 5: Printing photos-html-using-android

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>

Page 6: Printing photos-html-using-android

Create/Add Activity Name: PrintPdf.javaUse Following Code for PrintPdf.java

Code for PrintPdf.javaimport 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); }

Page 7: Printing photos-html-using-android

Create/Add Activity Name: PrintPdf.javaUse 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(); } }

Page 8: Printing photos-html-using-android

Create/Add Activity Name: PrintPdf.javaUse Following Code for PrintPdf.java

PrintPdf.java code continue….// Method for Image to PDF private void doPhotoPrint() { 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() {

PrintPdf.java code continue…. @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>";

webView.loadDataWithBaseURL(null, htmlDocument, "text/HTML", "UTF-8", null);

Page 9: Printing photos-html-using-android

Create/Add Activity Name: PrintPdf.javaUse 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 instancePrintManager 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.

Page 10: Printing photos-html-using-android

Manifest File CodeMain 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">

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-sdk android:minSdkVersion="19" android:targetSdkVersion="19"/>

<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>

New Classes used in the above code:

PrintHelper Helper for printing bitmaps. PrintHelperKitkat Kitkat specific PrintManager API implementation.

PrintManagerSystem level service for accessing the printing capabilities of the platform.

PrintDocumentAdapterBase class that provides the content of a document to be printed.

PrintJob This class represents a print job from the perspective of an application.

Page 11: Printing photos-html-using-android

App Screen with two options for Image and HTML Printing and PDF Generation

Page 12: Printing photos-html-using-android

Save as PDF Dialog Box, Enter Filename and Hit Save

Page 13: Printing photos-html-using-android

PDF Files Generated With AppFor more details on Printing Content using Android visit following link:https://developer.android.com/training/printing/index.html

HTML to PDF Image to PDF

Page 14: Printing photos-html-using-android

Contact Us

Lets Nurture UKGumption Business Centre

GlydegateBradford

West Yorkshire BD5 0BQ

United Kingdom +44(01274)271727

www.letsnurture.co.uk

Lets Nurture INDIA312 Kalasagar Mall

Nr. Satadhar Cross Road Ahmedabad

Gujarat380061 India

(+91) 7940095953www.letsnurture.com