title: Using the Google Cloud Print API with Android
body: <p>I am working on an android application that needs to print to a printer. I decided on using the Google Cloud Print, as it seemed easy to set up. Initially, I followed the steps found <a href="https://developers.google.com/cloud-print/docs/android" rel="noreferrer">here</a> for integrating into Android. This works, as in it will print to my desired printer. However, this process is a bit involved for the user. In my case, the process is as follows:</p>
  
  <ol>
  <li>The user selects the print button that I have displayed next to some information. </li>
  <li>A Dialog is shown with a preview of what will be printed. There is a button in the ActionBar that says "Print". This begins the process.</li>
  <li>A new Activity is displayed showing a list of printers that are connected to that users Google Account. The user must select one.</li>
  <li>A new page is shown giving a description of the print job. </li>
  <li>The user has to select "Print" in the upper right hand corner. </li>
  <li>The print job is started and the printer prints out the picture. </li>
  </ol>
  
  <p>Unfortunately, my client does not want this process. They want the user to click "Print" in step two, and then have the picture printed (steps 1, 2 and 6). Thus, I cannot use Intent provided by Google, I must use the actual API. This requires me to get a Google Auth token, get the desired printer, and submit a print job that way. I do the following: </p>
  
  <ol>
  <li>Use the Google Play Services to retrieve an OAuth token for the users Gmail account. </li>
  <li>Get a list of printers using the /search API call. </li>
  <li>Submit a print job using the /submit API call. </li>
  </ol>
  
  <p>I have the first two finished. I am just having trouble with the actual printing of the picture. Instead of printing the picture, the byte data of the picture is being printed (Base64 encoded). Here is some code as to how I am sending up the request: </p>
  
  <pre><code>ContentResolver contentResolver = context.getContentResolver();
  try {
      InputStream is = contentResolver.openInputStream(uri);
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
  
      byte[] buffer = new byte[4096];
      int n = is.read(buffer);
      while (n &gt;= 0) {
              baos.write(buffer, 0, n);
              n = is.read(buffer);
      }
      is.close();
      baos.flush();
  
      content = Base64.encodeToString(baos.toByteArray(), Base64.DEFAULT);
  } catch (FileNotFoundException e) {
      Log.d(TAG, "File not found: " + uri.toString(), e);
  } catch (IOException e) {
      e.printStackTrace();
  }
  </code></pre>
  
  <p>This code retrieves the picture (the variable "uri" is the URI of that file), and turns it into a Base64 encoded string. This is the same method used in the PrintDialogActivity that is provided on the Google Cloud Print page (linked to above). The following is how I send that up:</p>
  
  <ul>
  <li>URL: <a href="http://www.google.com/cloudprint/submit?access_token=[AUTH_TOKEN_GOES_HERE]&amp;cookies=false&amp;printerid=[PRINTER_ID_HERE]" rel="noreferrer">http://www.google.com/cloudprint/submit?access_token=[AUTH_TOKEN_GOES_HERE]&amp;cookies=false&amp;printerid=[PRINTER_ID_HERE]</a></li>
  <li>HTTP Method: POST</li>
  <li>POST Parameters: [printerId=PRINTER_ID_HERE, title=TestPrint, contentType=image/jpeg, capabilities={"capabilities":[{}]}, content=[Base64 Encoded data string is placed here]]</li>
  </ul>
  
  <p>As far as I can tell, this is how it is supposed to be. I am getting a response of {"success":true} when printing. But, as I said above, it prints out the actual Base64 data string. Any help would be appreciated. </p>
  
  <p><strong>EDIT:</strong> Using what powerje said below, I managed to fix this. Rather than using the code above, I used the following: </p>
  
  <pre><code>public void submitPrintJobWithFile(String printerId, String title, String token, String filePath, String contentType){
      File file = new File(filePath);
      // Method that gets the correct headers
      List&lt;Header&gt; headers = getHeaders(contentType, token);
      // Method that gets the correct post parameters
      String url = CLOUDPRINT_URL + PATH_SUBMIT;
      List&lt;NameValuePair&gt; postParams = getParams(title, contentType);
      String params = "access_token=" + token + "&amp;cookies=false" + "&amp;printerid=" + printerId;
      url += params;
      response = sendMultipartData(url, file, postParams, headers);
  }
  
  private String sendMultipartData(String url, File file, List&lt;NameValuePair&gt; fields, List&lt;Header&gt; headers){
      HttpPost post = new HttpPost(url);
      MultipartEntity entity = new MultipartEntity();
      for(NameValuePair pair : fields){
          String name = pair.getName();
          String value = pair.getValue();
          try{
              entity.addPart(name, new StringBody(value));
          }catch (UnsupportedEncodingException e){
              Log.d(TAG, "Error turning pair (name=" + name + ", value=" + value + ") into StringBody.");
      }
      entity.addPart("content", new FileBody(file));
      post.setEntity(entity);
      // Finish HttpClient request here...
  }
  </code></pre>