【问题标题】:Android Printing directly to network printer?Android 直接打印到网络打印机?
【发布时间】:2011-05-24 07:02:18
【问题描述】:

您好,在我的应用程序中,我想将我的数据从我的 Android 手机直接发送到我的网络打印机进行打印。我怎样才能做到这一点?

我还想提供布局、副本、页面范围等规范。如何直接从我的 Android 手机检测我的打印机并发出打印命令?

【问题讨论】:

  • 你在用蓝牙打印机吗???
  • 我已经编写了一个写入蓝牙打印机的应用程序。在这种情况下,您只需将手机与设备配对,然后打开一个套接字并向其写入数据。
  • @sujit 我正在使用网络打印机

标签: java android printing


【解决方案1】:

您只需将您的文档提交到谷歌云打印。这是我用来打印的代码。文档保存在外部存储中,格式为 pdf 格式。唯一的要求是设备和无线打印机必须在同一个网络中。如果打印机是有线的,安卓设备和打印机连接的系统必须用同一个google账号登录。

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);

【讨论】:

    猜你喜欢
    • 2012-11-02
    • 1970-01-01
    • 2011-06-24
    • 1970-01-01
    • 1970-01-01
    • 2017-04-21
    • 1970-01-01
    • 2012-05-28
    • 2011-04-13
    相关资源
    最近更新 更多