gpt4 book ai didi

java - Android 直接打印到网络打印机?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:08:13 24 4
gpt4 key购买 nike

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

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

最佳答案

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

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

关于java - Android 直接打印到网络打印机?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6106846/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com