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

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

相关内容

热门资讯

科技介绍"天天爱柳州... 科技介绍"天天爱柳州辅助器"开挂(透视)wepoker轻量版有透视吗(竟然有挂);天天爱柳州辅助器A...
技术分享"卡五星辅助... 技术分享"卡五星辅助器逍遥"开挂(透视)werplan辅助软件(有挂神器);卡五星辅助器逍遥是一种具...
必备教程"微信小程序... 必备教程"微信小程序多功能修改器"开挂(透视)wepokerplus脚本(有挂存在);亲真的是有正版...
普及知识"werpl... 普及知识"werplan开挂"开挂(透视)德普之星的辅助工具介绍(有挂方法);1、让任何用户在无需A...
每日必看推荐"赣牌圈... 每日必看推荐"赣牌圈小程序德隐藏机制"开挂(透视)wepoker提高好牌率(有挂攻略);超受欢迎的稳...
每日必看教程"新星游... 每日必看教程"新星游辅助软件"开挂(透视)hhpoker可以控制吗(有挂秘笈);AI辅助机器人普及解...
透视安装"广东雀神智... 透视安装"广东雀神智能插件智能价格"开挂(透视)pokemomo辅助软件(真的有挂);1、超多福利:...
记者爆料"相约十三张... 记者爆料"相约十三张辅助"开挂(透视)wepoker怎么挂飞机(有挂分析);相信小伙伴都知道这个相约...
教程攻略"拱趴大菠萝... 教程攻略"拱趴大菠萝怎么开挂"开挂(透视)agpoker辅助(真实有挂);软件透视开挂更新新赛季,主...
透视线上"微乐自建房... 透视线上"微乐自建房脚本使用安全吗"开挂(透视)hhpoker透视脚本(今日头条);人气非常高,ai...