33
A
回答
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
謝謝你,你節省了我噸的時間:) –
相關問題
- 1. 如何通過反射使用KitKat 4.4打印API打印WebView?
- 2. 如何使用Android 4.4打印API創建PrintDocumentAdapter以打印圖像
- 3. 使用pdf打印機打印文檔
- 4. 使用PrintManager直接打印PDF文件Android 4.4
- 5. 如何打印pdf?
- 6. 打印PDF雙面打印
- 7. 如何使用不帶打印機的默認打印機(VB Net)打印PDF
- 8. 如何打印PDF文件在Java中與打印對話框
- 9. 無需打印對話框打印Android
- 10. 使用Applescript打開打印隊列。 (或者:如何使用AppleScript打印PDF?)
- 11. 用java打印PDF
- 12. 使用mvc打印pdf
- 13. 使用vb.net打印PDF
- 14. 使用.net WebBrowser打印PDF?
- 15. 在android中打印pdf
- 16. Zebra PDF從Android打印
- 17. Android打印PDF文檔
- 18. PDF打印 - APEX
- 19. Ghostscript PDF打印
- 20. 如何使用Prolog打印PDF文件
- 21. 如何使用FPDF以PHP打印PDF。
- 22. 如何在angularJS中打印本地PDF而無需打開打印對話框
- 23. 使用iText打開PDF時自動打開打印對話框
- 24. 如何在打印PDF時不使用Bluebeam打開revu PDF的
- 25. 如何使用打印對話框打印文檔?
- 26. 同步pdf打印和標準打印
- 27. 打印PDF到網絡打印機
- 28. 打印到PDF打印機編程
- 29. Dymo框架條碼打印
- 30. Java文本打印框架
您的意思是在kitkat? – 2013-12-21 08:46:50
是@AvinashBabu –