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"poke... 1、透视app"pokermaster破解版"详细辅助力荐教程(软件方法);详细教程。2、poker...
透视辅助"wepok... 透视辅助"wepoker轻量版透视方法"详细辅助详细教程(到底有透视技巧);最新版2024是一款经典...
透视教学"德州透视插... 透视教学"德州透视插件"详细辅助高科技教程(模拟器哪个好用辅助);最新版2024是一款经典耐玩的益智...
透视教程"德扑圈透视... 透视教程"德扑圈透视挂"详细辅助介绍教程(可以提高运气教程);支持2-10人实时对战,虚拟庄家发牌,...
透视辅助"德扑之心免... 透视辅助"德扑之心免费透视"详细辅助安装教程(开脚本视频透视);支持2-10人实时对战,虚拟庄家发牌...
透视软件"aapok... 透视软件"aapoker透视方法"详细辅助可靠技巧(可以下载游戏技巧)科技教程也叫必备教程,这是一款...
透视辅助"xpoke... 这是一款非常优秀的xpoker辅助神器 ia辅助检测软件,能够让你了解到xpoker辅助神器中牌率当...
透视ai代打"佛手在... 透视ai代打"佛手在线大菠萝为什么都输"详细辅助wepoke教程(轻量版辅助方法);原来确实真的有挂...
透视好友房"hhpo... 透视好友房"hhpoker真的假的"详细辅助透视教程(可以看底牌透明);原来确实真的有挂(需添加指定...
透视软件"wepok... 您好,wepokerplus开挂这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】...