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软件是真的吗... 透视透视挂!wpk软件是真的吗,(WPK德州局)辅助软件(原来是有挂)1、首先打开wpk软件是真的吗...
透视ai!wpk有作弊吗,(w... 透视ai!wpk有作弊吗,(wPK模拟器)安卓下载辅助(竟然是真的有挂);1、wpk有作弊吗ai辅助...
透视好友!wpk辅助器,(wP... 透视好友!wpk辅助器,(wPk免费)辅助器(切实真的是有挂)1、全新机制【wpk辅助器软件透明挂】...
透视脚本!wpk辅助哪里买,(... 透视脚本!wpk辅助哪里买,(Wpk方法)竟然真的有挂(详细免费辅助)wpk辅助哪里买软件透明挂微扑...
透视有挂!wpk免费辅助,(w... 透视有挂!wpk免费辅助,(wpK免费)都是存在有挂(详细透视辅助)1、透视有挂!wpk免费辅助,(...
透视代打!wpk透视工作室,(... 透视代打!wpk透视工作室,(Wpk软件)真是是真的有挂(详细私人辅助);wpk透视工作室辅助器中分...
透视玄学!wpk免费辅助,(W... 透视玄学!wpk免费辅助,(WPk模拟器)官网下载链接(果然真的是有挂);1、任何wpk免费辅助ai...
透视美元局!wpk作弊,(wp... 透视美元局!wpk作弊,(wpK私人局)微扑克微乐辅助(都是是真的有挂)1、微扑克微乐辅助ai机器人...
透视玄学!wpk俱乐部怎么作弊... 透视玄学!wpk俱乐部怎么作弊,(WpK辅助)透视是真的假的(果然存在有挂)wpk俱乐部怎么作弊辅助...
透视黑科技!wpk真吗,(Wp... 透视黑科技!wpk真吗,(Wpk插件)免费辅助(都是真的有挂)1.wpk真吗 ai辅助创建新账号,点...