Google Cloud Print will no longer be supported as of December 31, 2020. Please see the support article for help migrating.

Android Integration

This document describes how to add printing functionality to any Android application by integrating with Google Cloud Print. The instructions below are for incorporating Google Cloud Print into your app when run on Android 4.3 and below. When on Android 4.4 and above, your app should instead use the Android Print Framework, which includes support for Google Cloud Print.

The integration process is simple and straightforward. It requires that you include the GCP Dialog intent implementation code in your project, start the intent from your code and pass the document to be printed. We currently provide the best support for PDF documents, so we encourage you to use this format over others. You can generate PDF using one of many open-source libraries (see here for a non-exhaustive list). You can also experiment with other formats (like JPEG, PNG, etc), but please expect to have occasional conversion failures. The following walk-through will help you get started:

Integration Steps

Begin by creating two new files in your Android project, print_dialog.xml and, as per the code below.

Next, add <uses-permission android:name="android.permission.INTERNET" /> to your application manifest.

Finally, add a "Print" element to your application's UI and add the following code in its onClick listener:

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

In the code above, replace the three parameters as follows:

  • docUri - URI of the document to be printed
  • docMimeType - MIME type of the document to be printed. We recommend that you use PDF (application/pdf) format
  • docTitle - title of the printed document, arbitrary string that will be shown on the GCP management console as the print job's title

Save the following code to a file named print_dialog.xml. It will form the layout for the print dialog to be shown within your application.

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

<RelativeLayout xmlns:android=""
  <WebView android:id="@+id/webview"

Save the following code to a file named It forms an Activity class to control the new printing functionality.


import android.content.ActivityNotFoundException;
import android.content.ContentResolver;
import android.content.Intent;
import android.os.Bundle;
import android.util.Base64;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;


public class PrintDialogActivity extends Activity {
  private static final String PRINT_DIALOG_URL = "";
  private static final String JS_INTERFACE = "AndroidPrintDialog";
  private static final String CONTENT_TRANSFER_ENCODING = "base64";

  private static final String ZXING_URL = "";
  private static final int ZXING_SCAN_REQUEST = 65743;

   * Post message that is sent by Print Dialog web page when the printing dialog
   * needs to be closed.
  private static final String CLOSE_POST_MESSAGE_NAME = "cp-dialog-on-close";

   * Web view element to show the printing dialog in.
  private WebView dialogWebView;

   * Intent that started the action.
  Intent cloudPrintIntent;

  public void onCreate(Bundle icicle) {

    dialogWebView = (WebView) findViewById(;
    cloudPrintIntent = this.getIntent();

    WebSettings settings = dialogWebView.getSettings();

    dialogWebView.setWebViewClient(new PrintDialogWebClient());
      new PrintDialogJavaScriptInterface(), JS_INTERFACE);


  public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if (requestCode == ZXING_SCAN_REQUEST && resultCode == RESULT_OK) {

  final class PrintDialogJavaScriptInterface {
    public String getType() {
      return cloudPrintIntent.getType();

    public String getTitle() {
      return cloudPrintIntent.getExtras().getString("title");

    public String getContent() {
      try {
        ContentResolver contentResolver = getContentResolver();
        InputStream is = contentResolver.openInputStream(cloudPrintIntent.getData());
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        byte[] buffer = new byte[4096];
        int n =;
        while (n >= 0) {
          baos.write(buffer, 0, n);
          n =;

        return Base64.encodeToString(baos.toByteArray(), Base64.DEFAULT);
      } catch (FileNotFoundException e) {
      } catch (IOException e) {
      return "";

    public String getEncoding() {

    public void onPostMessage(String message) {
      if (message.startsWith(CLOSE_POST_MESSAGE_NAME)) {

  private final class PrintDialogWebClient extends WebViewClient {
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
      if (url.startsWith(ZXING_URL)) {
        Intent intentScan = new Intent("");
        intentScan.putExtra("SCAN_MODE", "QR_CODE_MODE");
        try {
          startActivityForResult(intentScan, ZXING_SCAN_REQUEST);
        } catch (ActivityNotFoundException error) {
      } else {
      return false;

    public void onPageFinished(WebView view, String url) {
      if (PRINT_DIALOG_URL.equals(url)) {
        // Submit print document.
          + "window." + JS_INTERFACE + ".getType(),window." + JS_INTERFACE + ".getTitle(),"
          + "window." + JS_INTERFACE + ".getContent(),window." + JS_INTERFACE + ".getEncoding()))");

        // Add post messages listener.
            + "function(evt){window." + JS_INTERFACE + ".onPostMessage(}, false)");