Android 获取可用打印机(Wifi、蓝牙、USB)的列表,并使用所选打印机打印文档
我正在尝试获取可用打印机(Wifi、蓝牙、USB)的列表,并使用所选打印机打印文档。一些帖子建议使用PrinterDiscoverySession获取打印机列表,但我找不到一个合适的例子。Android 获取可用打印机(Wifi、蓝牙、USB)的列表,并使用所选打印机打印文档,android,Android,我正在尝试获取可用打印机(Wifi、蓝牙、USB)的列表,并使用所选打印机打印文档。一些帖子建议使用PrinterDiscoverySession获取打印机列表,但我找不到一个合适的例子。 任何帮助都将不胜感激。谢谢。对不起。我的英语写得不好 如果您有pdf文档,您可以将此代码用于所有打印: if(android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { PrintManager printMana
任何帮助都将不胜感激。谢谢。对不起。我的英语写得不好 如果您有pdf文档,您可以将此代码用于所有打印:
if(android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
PrintManager printManager = (PrintManager) Pdf_activity.this.getSystemService(Context.PRINT_SERVICE);
String jobName = "testpdf";
PrintDocumentAdapter pda = new PrintDocumentAdapter()
{
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onWrite(PageRange[] pages, ParcelFileDescriptor destination, CancellationSignal cancellationSignal, WriteResultCallback callback)
{
InputStream input = null;
OutputStream output = null;
try {
File file = new
File(getApplicationContext().getExternalFilesDir(null),"factore.pdf"); //direction your document
input = new FileInputStream(file);
output = new FileOutputStream(destination.getFileDescriptor());
byte[] buf = new byte[1024];
int bytesRead;
while ((bytesRead = input.read(buf)) > 0) {
output.write(buf, 0, bytesRead);
}
}
catch (Exception e) {
} finally {
try {
input.close();
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
callback.onWriteFinished(new PageRange[]{PageRange.ALL_PAGES});
}
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes, CancellationSignal cancellationSignal, LayoutResultCallback callback, Bundle extras)
{
if (cancellationSignal.isCanceled())
{
callback.onLayoutCancelled();
return;
}
//int pages = computePageCount(newAttributes);
PrintDocumentInfo pdi = new PrintDocumentInfo.Builder("Factore.pdf").setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT).build();
callback.onLayoutFinished(pdi, true);
}
};
printManager.print(jobName, pda, null);
}
这对我来说很好
但是如果从视图打印,首先从视图创建pdf,
我也有这些代码。
如果您想告诉我发送给您谢谢您的答复。但是,我想显示可用打印机的列表。请提供一些代码。是的,这个代码就是这样做的。显示所有可用的打印机,即使PDF这是默认的打印机制。我需要创建一个自定义屏幕,将得到所有的打印机,分类在Wifi,蓝牙,USB类别,然后从选定的打印机打印发票。为便于将来使用,应自动选择此打印机。