Java scrollView返回空位图的屏幕截图
我试图截取一个scrollview的所有内容的屏幕截图(也在页面外)。我遇到的问题是,当它到达行Java scrollView返回空位图的屏幕截图,java,android,bitmap,screenshot,Java,Android,Bitmap,Screenshot,我试图截取一个scrollview的所有内容的屏幕截图(也在页面外)。我遇到的问题是,当它到达行Bitmap b=u.getDrawingCache()时引发异常“nullPointerException”。我似乎不知道是什么原因造成的。这是我的代码: if(v.getId() == R.id.btnSnap) { try { View u = findViewById(R.id.svContent); u.
Bitmap b=u.getDrawingCache()时代码>引发异常“nullPointerException”。我似乎不知道是什么原因造成的。这是我的代码:
if(v.getId() == R.id.btnSnap)
{
try
{
View u = findViewById(R.id.svContent);
u.setDrawingCacheEnabled(true);
ScrollView z = (ScrollView) findViewById(R.id.svContent);
int totalHeight = z.getChildAt(0).getHeight();
int totalWidth = z.getChildAt(0).getWidth();
u.layout(0, 0, totalWidth, totalHeight);
u.buildDrawingCache(true);
Bitmap b = u.getDrawingCache();
//Just so that I can get a quick preview of
//what the picture looks like
ImageView img = (ImageView)findViewById(R.id.ivTest);
img.setImageBitmap(b);
//Save the picture
File fileName = new File(context.getExternalFilesDir(null)+"/ArcFlash/SafetyCheckList1.png");
FileOutputStream out = new FileOutputStream(fileName);
b.compress(Bitmap.CompressFormat.PNG, 90, out);
out.close();
}
catch(Exception ex)
{
handler.Log(ex.getMessage(), "onClick(SafetyCheckList.java)", context);
}
}
编辑问题似乎出现在u.layout(0,0,totalWidth,totalHeight)上代码>我不知道getDrawingCache()
还能用了。让您的滚动视图
呈现为位图
背景的画布
。它不能捕获包括屏幕外内容在内的所有内容吗?基本上,我所要做的就是将它作为png存储在设备上,这样我以后可以查看它“它不工作,因为它不能捕获所有内容,包括屏幕外的内容?”--我的印象是,硬件层的使用使图形缓存方法在一般情况下不再有效。在我尝试过的其他实现中,它可以工作并获得位图。我很好奇,在到达该行之前的任何步骤是否会导致空值。