Android Printing Directly to a Network Printer?

Hii in my application I want to send my data directly to my network printer from my Android phone to print it. How can i do this?

I also want to provide specifications such as layout, copies, page range, etc. How can I detect my printers directly from my Android phone and also issue a print command?

+5
source share
1 answer

You just need to send your documentation to Google Cloud Print. Here is the code I used to print. The document is stored in external storage and is in pdf format. The only requirement is that both the device and the wireless printer must be on the same network. If the printer is connected, the android device and the system connected to the printer must log in with the same google account.

PrintManager printManager = (PrintManager) Order_Bag.this.getSystemService(Context.PRINT_SERVICE);
String jobName = Order_Bag.this.getString(R.string.app_name) + " Document";
//printManager.print(jobName, pda, null);
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(Environment.getExternalStorageDirectory() + "/hello.pdf");
            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;
        }

        //    int pages = computePageCount(newAttributes);

        PrintDocumentInfo pdi = new PrintDocumentInfo.Builder("The invoice").setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT).build();

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

printManager.print(jobName, pda, null);
+1
source

All Articles