如何更改Android打印类中的默认打印选项

时间:2017-03-07 05:30:48

标签: android android-print-framework

我在Android应用程序中编写了一个打印类,我希望在旋转之前保存printAttribute并使用以前的printAttribute重新连接到打印机,但是当我传递oldPrintAttribute它不起作用时,打印对话框仍显示默认选项,这是我的代码。

PrintManager printManager = (PrintManager) context.getSystemService(Context.PRINT_SERVICE);
String jobName = PdfFragment.sProjectPrefix + " " + getFileNameFromPath(mPdfDocumentName);
printManager.print(jobName, new PdfFragmentPrintDocumentAdapter(), printAttributes);

或者我可以逐个在程序中设置打印选项吗?

2 个答案:

答案 0 :(得分:0)

您是否在Android N上测试了代码?由于此Android issue,因此在N之前忽略了printAttributes值。

答案 1 :(得分:0)

在传递到打印功能之前,先设置您的 PrintAttributes 。这对我有用。

PrintManager printManager = (PrintManager) context.getSystemService(PRINT_SERVICE);
    PrintAttributes newAttributes = new PrintAttributes.Builder().
            setMediaSize(PrintAttributes.MediaSize.ISO_A4).
            setMinMargins(PrintAttributes.Margins.NO_MARGINS).
            build();
    printManager.print(context.getString(R.string.print_job_name),
            new PdfFragmentPrintDocumentAdapter(context, view), newAttributes);

PdfFragmentPrintDocumentAdapter.java

public class PdfFragmentPrintDocumentAdapter extends PrintDocumentAdapter {

private PrintedPdfDocument document;
private Context context;
private View view;

public PdfFragmentPrintDocumentAdapter(Context context, View view) {
    this.context = context;
    this.view = view;
}

@Override
public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes,
                     CancellationSignal cancellationSignal,
                     LayoutResultCallback callback, Bundle extras) {
    document = new PrintedPdfDocument(context, newAttributes);
    if (cancellationSignal.isCanceled()) {
        callback.onLayoutCancelled();
        return;
    }

    PrintDocumentInfo.Builder builder = new PrintDocumentInfo
            .Builder(context.getString(R.string.pdf_file_name)+".pdf")
            .setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT)
            .setPageCount(1);

    PrintDocumentInfo info = builder.build();
    callback.onLayoutFinished(info, true);
}

@Override
public void onWrite(PageRange[] pages, ParcelFileDescriptor destination,
                    CancellationSignal cancellationSignal,
                    PrintDocumentAdapter.WriteResultCallback callback) {
    cancellationSignal.setOnCancelListener(new CancellationSignal.OnCancelListener() {
        @Override
        public void onCancel() {
            Toast.makeText(context, context.getString(R.string.printing_cancel), Toast.LENGTH_SHORT).show();
        }
    });

    PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(view.getWidth(),view.getHeight(), 1).create();
    PdfDocument.Page page = document.startPage(pageInfo);
    view.draw(page.getCanvas());
    document.finishPage(page);

    try {
        document.writeTo(new FileOutputStream(
                destination.getFileDescriptor()));
    } catch (IOException e) {
        String exception = e.toString();
        Toast.makeText(context, context.getString(R.string.printing_failed)+"\n" + exception, Toast.LENGTH_SHORT).show();
        callback.onWriteFailed(exception);
        return;
    } finally {
        document.close();
        document = null;
    }
    callback.onWriteFinished(new PageRange[]{new PageRange(0, 0)});
}

@Override
public void onFinish() {
    super.onFinish();
}

}