Reputation: 93
I wrote a service in android app to read a pdf file and send to the print using print manager. but it not allowing to print, and it says " IllegalStateException : Can print only from an activity".IS this a problem of newer android versions?
This is the cade
PrintManager printManager = (PrintManager) this.getSystemService(Context.PRINT_SERVICE);
PrintDocumentAdapter printAdapter = new PdfDocumentAdapter(pathPdfRead);
String jobName = getString(R.string.app_name) + newDoc;
printManager.print(jobName, printAdapter, new PrintAttributes.Builder().build());
I'm expecting some answer. Is this possible in Android 11 or higher versions?
Upvotes: 1
Views: 272
Reputation: 1037
The exception is self-explanatory:
IllegalStateException : Can print only from an activity
So if you do such implementation in a Service, the this
in this line is not an Activity Context:
PrintManager printManager = (PrintManager)this.getSystemService(Context.PRINT_SERVICE);
You must have an Activity Context in order to make it work.
Upvotes: 1