问题
I write a print class in Android app, and I want to save the printAttribute before rotation and reconnect to the printer using the former printAttribute, but when I pass the oldPrintAttribute it does not work, the print dialog still shows the default option, this is my code.
PrintManager printManager = (PrintManager) context.getSystemService(Context.PRINT_SERVICE);
String jobName = PdfFragment.sProjectPrefix + " " + getFileNameFromPath(mPdfDocumentName);
printManager.print(jobName, new PdfFragmentPrintDocumentAdapter(), printAttributes);
Or can I set the print option in my program one by one?
回答1:
Have you tested your code on Android N? The printAttributes values were ignored before N due to this Android issue.
回答2:
Set your PrintAttributes before passing to print function. This worked worked for me.
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();
}
}
来源:https://stackoverflow.com/questions/42640897/how-to-change-default-print-option-in-android-print-class