Android是否会创建类似Windows中的中间打印文件(即spool文件)?
创始人
2024-10-10 12:01:03
0

Android系统中使用的打印框架是CUPS(Common Unix Printing System)。CUPS在打印时会使用一个缓存文件夹来存储打印任务的中间文件,而这些文件可以被视为Android系统中的“中间打印文件”。

以下是一个示例代码,用于在Android应用程序中打印一个文本文件,并获取中间文件的路径:

// Set up a print job with a file to print
PrintJob job = new PrintJob();
job.setPrintService(printService);
job.setJobName("My Printing Job");

PrintDocumentAdapter adapter = new PrintDocumentAdapter() {
    @Override
    public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes, CancellationSignal cancellationSignal, LayoutResultCallback callback, Bundle extras) {
        // Respond to layout requests
    }

    @Override
    public void onWrite(PageRange[] pages, ParcelFileDescriptor destination, CancellationSignal cancellationSignal, WriteResultCallback callback) {
        // Write the document content to the destination file
        FileInputStream input = new FileInputStream(fileToPrint);
        FileOutputStream output = new FileOutputStream(destination.getFileDescriptor());

        byte[] buf = new byte[1024];
        int bytesRead;
        try {
            while ((bytesRead = input.read(buf)) > 0) {
                output.write(buf, 0, bytesRead);
            }
            callback.onWriteFinished(new PageRange[]{PageRange.ALL_PAGES});
        } catch (IOException e) {
            Log.e(TAG, "Error printing file", e);
        } finally {
            try {
                input.close();
                output.close();
            } catch (IOException e) {
                Log.e(TAG, "Error closing streams", e);
            }
        }
    }
};

PrintAttributes attributes = new PrintAttributes.Builder()
        .setMediaSize(PrintAttributes.MediaSize.ISO_A4)
        .setColorMode(PrintAttributes.COLOR_MODE_COLOR)
        .setResolution(new PrintAttributes.Resolution("pdf", "pdf", 600, 600))
        .setMinMargins(PrintAttributes.Margins.NO_MARGINS)
        .build();

// Send the print job to the print service
PrintJobInfo info = job.createPrintJobInfo();
info.setAttributes(attributes);
info.setCopies(1);
printService.onPrintJobQueued(info);

// Get the path to the intermediate file
String cacheDir = getApplicationContext().getCacheDir().getAbsolutePath();
String jobId = info.getId().toString();
String filename = String.format("cups-job-%s.pdf", jobId);
String intermediateFilePath = String.format("%s/%s", cacheDir, filename);

此代码创建了一个打印作业,并将其

相关内容

热门资讯

微扑克有辅助透视!微扑克辅助测... 微扑克有辅助透视!微扑克辅助测试,wepoke计算辅助,教你攻略(确实真的有挂);1.微扑克辅助测试...
黑科技规律!(WPK)透视辅助... 黑科技规律!(WPK)透视辅助app,(Wpk)先前存在有挂,规律教程(有挂软件)这是由厦门游乐互动...
微扑克游戏辅助器!aapoke... 自定义微扑克游戏辅助器系统规律,只需要输入自己想要的开挂功能,一键便可以生成出微扑克专用辅助器,不管...
黑科技安卓版!(aapOKER... 黑科技安卓版!(aapOKER)透明挂辅助插件,(aAPOKER)原本真的是有挂,AI教程(有挂透视...
黑科技好友!(WEPoke)软... 黑科技好友!(WEPoke)软件透明挂,(wePOke)好像是真的有挂,教你教程(有挂透视);1分钟...
wpk职业代打!微扑克透明挂,... 1、wpk职业代打!微扑克透明挂,poker master安卓版外挂,细节揭秘(本然是有挂)2、进入...
黑科技透明挂!(微扑克)透视辅... 黑科技透明挂!(微扑克)透视辅助脚本,(微扑克wpk)原先是有挂,辅助教程(有挂攻略)1、很好的工具...
wepoke黑科技!wopok... 这是一款非常优秀的wpk微扑克俱乐部 ia辅助检测软件,能够让你了解到wpk微扑克俱乐部中牌率当中全...
wepoke辅助!微扑克发牌规... wepoke辅助!微扑克发牌规律性总结,impoker有挂吗,2025新版总结(确实是有挂);玩家必...
黑科技安装!(wPK)透视辅助... 1、黑科技安装!(wPK)透视辅助app,(WPk)先前是真的有挂,新版2025教程(有挂方法)2、...