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

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

相关内容

热门资讯

有玩家发现!约局吧德州有挂吗,... 有玩家发现!约局吧德州有挂吗,至友互娱情怀辅助插件,手段辅助攻略(有挂教学)-哔哩哔哩;1、上手简单...
今天上午!欢乐达人暗堡链接脚本... 今天上午!欢乐达人暗堡链接脚本(辅助)真是是有辅助app(果真有挂)-哔哩哔哩亲,关键说明,欢乐达人...
透视免费!拱趴大菠萝万能挂,微... 透视免费!拱趴大菠萝万能挂,微信小程序家乡大贰修改器下载,指引辅助挂(有挂解惑)-哔哩哔哩1、玩家可...
随着!小闲南川手游辅助器(辅助... 随着!小闲南川手游辅助器(辅助)原来是有辅助软件(有挂规律)-哔哩哔哩在进入小闲南川手游辅助器软件靠...
值得注意的是!werplan辅... 值得注意的是!werplan辅助软件,如何使用掌酷十三张脚本,方式辅助技巧(有挂秘诀)-哔哩哔哩1)...
为了进一步!随意玩h5透视辅助... 为了进一步!随意玩h5透视辅助(辅助)切实存在有辅助器(有挂透视)-哔哩哔哩随意玩h5透视辅助辅助器...
透视代打!poker worl... 透视代打!poker world辅助,指尖捕鱼bgm,演示辅助工具(有挂神器)-哔哩哔哩1、任何po...
目前来看!湖南牵手胡子跑改脚本... 目前来看!湖南牵手胡子跑改脚本(辅助)果然真的是有辅助修改器(了解有挂)-哔哩哔哩1、下载好湖南牵手...
透视玄学!约局吧开挂,poke... 透视玄学!约局吧开挂,pokermaster修改器,绝活儿辅助app(有挂技巧)-哔哩哔哩1、每一步...
据相关数据显示!新天道怎么看底... 据相关数据显示!新天道怎么看底牌(辅助)一直是有辅助工具(有挂秘诀)-哔哩哔哩新天道怎么看底牌是不是...