1

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

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

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

4

2 回答 2

1

在传递给打印功能之前设置您的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();
}

}

于 2019-01-03T11:36:45.427 回答
0

您是否在 Android N 上测试过您的代码?由于这个Android 问题,在 N 之前忽略了 printAttributes 值。

于 2017-03-14T21:05:30.800 回答