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

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

相关内容

热门资讯

透视了解!德普之星辅助器app... 透视了解!德普之星辅助器app,epoker免费透视脚本,2025教程(从来是有挂);科技详细教程Q...
透视脚本!wepoker底牌透... 透视脚本!wepoker底牌透视脚本怎样安装,哈糖大菠萝怎么挂,辅助教程(切实是有挂);支持2-10...
透视ai代打!aapoker透... 透视ai代打!aapoker透视脚本下载,德普之星有辅助软件吗,新2025版(本然是真的有挂);亲真...
透视智能ai!aapoker有... 透视智能ai!aapoker有脚本吗,来玩app破解版,安装教程(从前真的有挂);1.aapoker...
透视了解!wpk辅助工具,哈糖... 透视了解!wpk辅助工具,哈糖大菠萝能开挂吗,安装教程(果然是有挂);原来确实真的有挂(需添加指定Q...
透视ai!wpk辅助器,aap... 透视ai!wpk辅助器,aapoker透视脚本,透牌教程(原先是真的有挂);原来确实真的有挂(需添加...
透视ai代打!hhpoker辅... 透视ai代打!hhpoker辅助软件下载,hhpoker外挂靠谱吗,2025版教程(一向存在有挂);...
透视数据!aapoker辅助工... 透视数据!aapoker辅助工具存在吗,xpoker辅助工具,力荐教程(起初真的是有挂);(需添加指...
透视透视!hhpoker透视脚... 透视透视!hhpoker透视脚本视频,hhpoker俱乐部是干嘛的,解密教程(原来真的有挂)1、不需...
透视苹果版!wpk透视辅助,w... 透视苹果版!wpk透视辅助,wepoker辅助是真的假的,扑克教程(总是存在有挂)1、玩家可以在wp...