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

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

相关内容

热门资讯

透视玄学!wpk辅助,wepo... 透视玄学!wpk辅助,wepoker代打辅助机器人,技法教程(果然真的有挂)-哔哩哔哩wepoker...
透视软件!wepoker辅助器... 透视软件!wepoker辅助器下载(透视)开挂辅助工具(其实存在有挂)-哔哩哔哩小薇(透视辅助)致您...
透视数据!agpoker辅助,... 透视数据!agpoker辅助,微信小程序微乐陕西挖坑辅助,一直真的是有挂(必备曝光教程)-哔哩哔哩1...
透视肯定!pokerworld... 透视肯定!pokerworld修改器,wepoker数据分析工具,方式教程(果然真的是有挂)-哔哩哔...
透视稳赢!wpk辅助购买(透视... 透视稳赢!wpk辅助购买(透视)开挂辅助挂(果然真的是有挂)-哔哩哔哩1、任何wpk辅助购买ai辅助...
透视辅助!wepokerplu... 透视辅助!wepokerplus脚本,佛手在线大菠萝智能辅助器,果然是真的挂(解谜扑克教程)-哔哩哔...
透视智能ai!wpk辅助是什么... 透视智能ai!wpk辅助是什么,hhpoker到底可以辅助码,模板教程(都是真的有挂)-哔哩哔哩1、...
透视辅助挂!大菠萝免费辅助(透... 透视辅助挂!大菠萝免费辅助(透视)开挂辅助方法(确实是有挂)-哔哩哔哩1、大菠萝免费辅助ai机器人多...
据悉!wepoker看底牌,逗... 据悉!wepoker看底牌,逗娱碰胡开挂,确实是有挂(总结大神讲解)-哔哩哔哩1、实时逗娱碰胡开挂开...
透视新版!德扑HHpoker有... 透视新版!德扑HHpoker有挂吗,pokemmo手机版透视脚本,绝活教程(真是真的有挂)-哔哩哔哩...