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一贯是真的有挂,解说技巧(有挂ai代打)1、每个玩家都可以进...
wepokeai代打!wepo... wepokeai代打!wepoke软件透明挂,wepoke辅助真的假的,2025新版教程(有挂规律)...
必备辅助推荐!广东老友麻将有没... 必备辅助推荐!广东老友麻将有没有挂(透视)都是是有挂(2025已更新)(哔哩哔哩)1.广东老友麻将有...
微扑克有辅助挂!微扑克辅助钻石... 微扑克有辅助挂!微扑克辅助钻石,微扑克竟然是真的有挂,科技教程(有挂工具)是一款可以让一直输的玩家,...
wepoke有挂!德州wepo... wepoke有挂!德州wepower透视辅助,wepoke显示有,微扑克教程(有挂工具);原来确实真...
玩家爆料!东游麻将中挂件(辅助... 玩家爆料!东游麻将中挂件(辅助)一贯有挂(2024已更新)(哔哩哔哩);1、金币登录送、破产送、升级...
微扑克ai辅助!微扑克ai人工... 一、微扑克简介了解软件请加微:136704302微扑克是一款在线扑克游戏平台,玩家可以在平台上进行多...
透牌教程!wepoke游戏辅助... 透牌教程!wepoke游戏辅助工具(wepoke辅助挂)WepOke(一贯真的有挂);科技详细教程小...
分享实测!胡乐辅助器免费版(辅... 分享实测!胡乐辅助器免费版(辅助挂)竟然有挂(2025已更新)(哔哩哔哩)1、玩家可以在胡乐辅助器免...
微扑克辅助机器人!wpk微扑克... 微扑克辅助机器人!wpk微扑克辅助透视,微扑克ai辅助工具,细节揭秘(有挂技巧);微扑克是一种具有地...