Android Printooth错误不存在';t print image试图对空对象引用调用虚方法
我正在尝试使用Printooth将滚动视图布局打印到蓝牙打印机。我尝试使用getBitmapFromView()方法将布局转换为位图,并使用以下方法打印Android Printooth错误不存在';t print image试图对空对象引用调用虚方法,android,Android,我正在尝试使用Printooth将滚动视图布局打印到蓝牙打印机。我尝试使用getBitmapFromView()方法将布局转换为位图,并使用以下方法打印 printables.add(new ImagePrintable.Builder(b).build()); printing.print(printables); 但是当我试图打印它时,应用程序什么都不做,并且出现了这个错误, 有人能帮我解决这个问题吗 Attempt to invoke virtual method '
printables.add(new ImagePrintable.Builder(b).build());
printing.print(printables);
但是当我试图打印它时,应用程序什么都不做,并且出现了这个错误,
有人能帮我解决这个问题吗
Attempt to invoke virtual method 'void com.mazenrashed.printooth.utilities.Printing.print(java.util.ArrayList)' on a null object reference
代码如下:
private void takeScreenShot() {
File folder = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/ScreenShot/");
if (!folder.exists()) {
boolean success = folder.mkdir();
}
path = folder.getAbsolutePath();
path = path + "/" + file_name + System.currentTimeMillis() + ".pdf";
// List<Integer> hights = new ArrayList<>();
View u = findViewById(R.id.scroll);
NestedScrollView z = (NestedScrollView) findViewById(R.id.scroll);
totalHeight = z.getChildAt(0).getHeight();
totalWidth = z.getChildAt(0).getWidth();
Log.e("totalHeight--", "" + totalHeight);
Log.e("totalWidth--", "" + totalWidth);
//Save bitmap
String extr = Environment.getExternalStorageDirectory() + "/ScreenShot/";
File file = new File(extr);
if (!file.exists())
file.mkdir();
fileName = file_name + ".jpg";
myPath = new File(extr, fileName);
imagesUri = myPath.getPath();
FileOutputStream fos = null;
b = getBitmapFromView(u, totalHeight, totalWidth);
try {
fos = new FileOutputStream(myPath);
b.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// createPdf();
final ArrayList<Printable> printables = new ArrayList<>();
printables.add(new ImagePrintable.Builder(b).build());
printing.print(printables);
}
public Bitmap getBitmapFromView(View view, int totalHeight, int totalWidth) {
Bitmap returnedBitmap = Bitmap.createBitmap(totalWidth, totalHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(returnedBitmap);
Drawable bgDrawable = view.getBackground();
if (bgDrawable != null)
bgDrawable.draw(canvas);
else
canvas.drawColor(Color.WHITE);
view.draw(canvas);
toGrayscale(returnedBitmap);
return returnedBitmap;
}
public static Bitmap toGrayscale(Bitmap srcImage) {
Bitmap bmpGrayscale = Bitmap.createBitmap(srcImage.getWidth(),
srcImage.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bmpGrayscale);
Paint paint = new Paint();
ColorMatrix cm = new ColorMatrix();
cm.setSaturation(0);
paint.setColorFilter(new ColorMatrixColorFilter(cm));
canvas.drawBitmap(srcImage, 0, 0, paint);
return bmpGrayscale;
}
private void takeScreenShot(){
File folder=新文件(Environment.getExternalStorageDirectory().getAbsolutePath()+“/ScreenShot/”;
如果(!folder.exists()){
布尔成功=folder.mkdir();
}
path=folder.getAbsolutePath();
path=path+“/”+文件名+System.currentTimeMillis()+“.pdf”;
//列表高度=新的ArrayList();
视图u=findViewById(R.id.scroll);
NestedScrollView z=(NestedScrollView)findViewById(R.id.scroll);
totalHeight=z.getChildAt(0.getHeight();
totalWidth=z.getChildAt(0).getWidth();
Log.e(“总高度--”和“+”总高度);
Log.e(“totalWidth--”和“+totalWidth”);
//保存位图
字符串extr=Environment.getExternalStorageDirectory()+“/ScreenShot/”;
文件=新文件(extr);
如果(!file.exists())
mkdir()文件;
文件名=文件名+“.jpg”;
myPath=新文件(extr,文件名);
imagesUri=myPath.getPath();
FileOutputStream=null;
b=getBitmapFromView(u、总高度、总宽度);
试一试{
fos=新文件输出流(myPath);
b、 压缩(Bitmap.CompressFormat.PNG,100,fos);
fos.flush();
fos.close();
}捕获(例外e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
//createPdf();
最终ArrayList可打印文件=新ArrayList();
add(新的ImagePrintable.Builder(b.build());
打印。打印(可打印);
}
公共位图getBitmapFromView(视图视图,int totalHeight,int totalWidth){
Bitmap returnedBitmap=Bitmap.createBitmap(totalWidth、totalHeight、Bitmap.Config.ARGB_8888);
画布画布=新画布(返回的位图);
Drawable-bgDrawable=view.getBackground();
如果(bgDrawable!=null)
bgDrawable.draw(画布);
其他的
画布。drawColor(颜色。白色);
视图.绘制(画布);
toGrayscale(返回的位图);
返回位图;
}
公共静态位图toGrayscale(位图srcImage){
位图bmpGrayscale=Bitmap.createBitmap(srcImage.getWidth(),
srcImage.getHeight(),Bitmap.Config.ARGB_8888);
画布画布=新画布(bmpGrayscale);
油漆=新油漆();
ColorMatrix cm=新的ColorMatrix();
cm.设定饱和度(0);
paint.setColorFilter(新的ColorMatrixColorFilter(cm));
drawBitmap(srcImage,0,0,paint);
返回bmpGrayscale;
}
这是我要打印的滚动视图布局:
改变
printing.print(printables);
到
Printooth.INSTANCE.printer().print(printables);