2013-12-21 28 views

答えて

61

Googleで時間を過ごした後、私は解決策を見つけました。

PrintManager printManager = (PrintManager) this.getSystemService(Context.PRINT_SERVICE); 
String jobName = this.getString(R.string.app_name) + " Document"; 
printManager.print(jobName, pda, null); 

PrintDocumentAdapter pda = new PrintDocumentAdapter(){ 

    @Override 
    public void onWrite(PageRange[] pages, ParcelFileDescriptor destination, CancellationSignal cancellationSignal, WriteResultCallback callback){ 
     InputStream input = null; 
     OutputStream output = null; 

     try { 

      input = new FileInputStream(file to print); 
      output = new FileOutputStream(destination.getFileDescriptor()); 

      byte[] buf = new byte[1024]; 
      int bytesRead; 

      while ((bytesRead = input.read(buf)) > 0) { 
       output.write(buf, 0, bytesRead); 
      } 

      callback.onWriteFinished(new PageRange[]{PageRange.ALL_PAGES}); 

     } catch (FileNotFoundException ee){ 
      //Catch exception 
     } catch (Exception e) { 
      //Catch exception 
     } finally { 
      try { 
       input.close(); 
       output.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 

    @Override 
    public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes, CancellationSignal cancellationSignal, LayoutResultCallback callback, Bundle extras){ 

     if (cancellationSignal.isCanceled()) { 
      callback.onLayoutCancelled(); 
      return; 
     } 


     PrintDocumentInfo pdi = new PrintDocumentInfo.Builder("Name of file").setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT).build(); 

     callback.onLayoutFinished(pdi, true); 
    } 
}; 
+0

どのような種類のプリンタがサポートされていますか? – abhishek

+0

@abhishek http://www.google.com/cloudprint/learn/printers.html –

+0

ありがとうございました。時間が節約できました:) –

関連する問題