FATAL EXCEPTION: ec.kat.kataudit, PID: 4052 java.lang.IllegalStateException: Can print only from an activity
解决方法:attachBaseContext
private Context primaryBaseActivity;//THIS WILL KEEP ORIGINAL INSTANCE
@Override
protected void attachBaseContext(Context newBase) {
primaryBaseActivity=newBase;//SAVE ORIGINAL INSTANCE
/*Some locale handling stuff right here*/
/*LocaleHelper's onAttach is returning a *new* context in Android N which will void PrintManager's context*/
//super.attachBaseContext(LocaleHelper.onAttach(newBase,appLocale));
super.attachBaseContext(newBase);
}
public void print(Bitmap bitmap) {
// Get the print manager.
//PrintManager printManager = (PrintManager) this.getSystemService(Context.PRINT_SERVICE);
if(primaryBaseActivity == null){
primaryBaseActivity = this;
}
PrintHelper printHelper = new PrintHelper(primaryBaseActivity);
// Set the desired scale mode.
printHelper.setScaleMode(PrintHelper.SCALE_MODE_FIT);
printHelper.setOrientation(PrintHelper.ORIENTATION_PORTRAIT);
printHelper.setColorMode(PrintHelper.COLOR_MODE_COLOR);
// Get the bitmap for the ImageView's drawable.
//Bitmap bitmap = ((BitmapDrawable) mImageView.getDrawable()).getBitmap();
// Print the bitmap.
if(bitmap != null)
printHelper.printBitmap("Print Bitmap", bitmap);
}