Reputation: 856
I'm trying to get a list of available printers (Wifi, bluetooth, USBs) and print the document using the selected printer. A few posts suggest using PrinterDiscoverySession to get the list of printers, but I couldn't find a suitable example for the same. Any help would be appreciated. Thanks.
Upvotes: 0
Views: 665
Reputation: 612
excuse me . i cant writing English good .
if you have pdf document you can use this code for all print :
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);
}
this work for me good.
but if you print from your view first create pdf from your view , I also have those codes . If you want to tell me to send you
Upvotes: 1