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

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

相关内容

热门资讯

五分钟透明!博乐温州棋牌有没有... 这是一款非常优秀的博乐温州棋牌有没有挂 ia辅助检测软件,能够让你了解到博乐温州棋牌有没有挂中牌率当...
六分钟辅助挂!博雅红河棋盘外挂... 1、六分钟辅助挂!博雅红河棋盘外挂,微扑克智能助手(详细透视辅助挂教程);详细教程。2、博雅红河棋盘...
6分钟黑科技!友友邳州麻将辅助... 6分钟黑科技!友友邳州麻将辅助器,微扑克靠谱(详细透视辅助软件教程);实战中需综合运用上述技巧,并根...
4分钟攻略!传送屋有外 挂,德... 4分钟攻略!传送屋有外 挂,德扑之星记分牌有有用(详细透视辅助挂教程);传送屋有外 挂黑科技是一款具...
5分钟了解!浙江游戏大厅辅具器... 5分钟了解!浙江游戏大厅辅具器,来玩app辅助工具(详细透视辅助器教程);(需添加指定薇757446...
7分钟介绍!广东雀神辅助器怎么... 7分钟介绍!广东雀神辅助器怎么买,微扑克职业代打(详细透视辅助助手教程);亲,其实确实真的有挂(需添...
三分钟攻略!可乐斗地主有没有挂... 三分钟攻略!可乐斗地主有没有挂,智星德州菠萝在哪下载(详细透视辅助神器教程);可乐斗地主有没有挂黑科...
九分钟规律!花花生活圈脚本,云... 九分钟规律!花花生活圈脚本,云扑克有辅助软件(详细透视辅助软件教程);(需添加指定薇75744690...
七分钟总结!中至窝龙必赢攻略,... 相信很多朋友都在电脑上玩过中至窝龙必赢攻略吧,但是很多朋友都在抱怨用电脑玩起来不方便。为此小编给大家...
1分钟详情!六瓣数字消有没有挂... 1分钟详情!六瓣数字消有没有挂,cloudpoker辅助器(详细透视辅助工具教程)关于六瓣数字消有没...