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、这是跨平台的心悦填大坑辅...
第二分钟教你!乐乐围棋入门辅助... 第二分钟教你!乐乐围棋入门辅助,一起宁德钓蟹辅助器(竟然存在有挂)-哔哩哔哩1、让任何用户在无需一起...
第九分钟关于!蜀山四川辅助脚本... 您好,蜀山四川辅助脚本多少钱这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054】...
第五分钟揭露!大菠萝789辅助... 第五分钟揭露!大菠萝789辅助器下载,南昌微乐自建房辅助(好像是真的挂)-哔哩哔哩1、许多玩家不知道...
第5分钟解密!九酷众游软件,金... 第5分钟解密!九酷众游软件,金杯竞技辅助(都是存在有挂)-哔哩哔哩1、这是跨平台的九酷众游软件黑科技...
第八分钟推荐!传送屋激k如何开... 第八分钟推荐!传送屋激k如何开启透视,新二号辅助下载(果然存在有挂)-哔哩哔哩1、传送屋激k如何开启...
第九分钟详细!闲逸透视软件是免... 第九分钟详细!闲逸透视软件是免费的吗,抓住捣蛋鸡开挂(竟然是真的挂)-哔哩哔哩闲逸透视软件是免费的吗...
9分钟详细!一起宁德钓蟹脚本,... 9分钟详细!一起宁德钓蟹脚本,微信小程序微乐辅助免费(真是是有挂)-哔哩哔哩1、玩家可以在微信小程序...
三分钟推荐!微乐小程序辅助免费... 三分钟推荐!微乐小程序辅助免费,微信小程序微乐辅助免费(其实存在有挂)-哔哩哔哩1、微信小程序微乐辅...