Below Code causing PrintSpooler Crash and Not Printing at All.
private void createWebPrintJob(WebView webView) {
// Get a PrintManager instance
PrintManager printManager = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
printManager = (PrintManager) getBaseContext()
.getSystemService(Context.PRINT_SERVICE);
}
String jobName = getString(R.string.app_name) + " Document";
// Get a print adapter instance
PrintDocumentAdapter printAdapter = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
printAdapter = webView.createPrintDocumentAdapter(jobName);
}
// Create a print job with name and adapter instance
PrintJob printJob = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
printJob = printManager.print(jobName, printAdapter,
new PrintAttributes.Builder().build());
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
printJob.restart();
}
// Save the job object for later status checking
//mPrintJobs.add(printJob);
}
private WebView mWebView;
private void doWebViewPrint() {
// Create a WebView object specifically for printing
WebView webView = new WebView(getApplicationContext());
webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
@Override
public void onPageFinished(WebView view, String url) {
createWebPrintJob(view);
mWebView = null;
}
});
// Generate an HTML document on the fly:
String htmlDocument = "<html><body><h1>Test Content</h1>
Testing, " +
"testing, testing...
</body></html>";
webView.loadDataWithBaseURL(null, HtmlEmail, "text/HTML", "UTF-8", null);
// Keep a reference to WebView object until you pass the PrintDocumentAdapter
// to the PrintManager
mWebView = webView;
}