我想获取tableview的屏幕截图并将其转换为位图图像以与其他人共享。 这是我的getscreen()函数。
private File getScreen() {
View view = (TableLayout) findViewById(R.id.tablelayout1);
view.setDrawingCacheEnabled(true);
view.layout(0, 0, view.getWidth(), view.getHeight());
view.buildDrawingCache(true);
view.setBackgroundColor(Color.WHITE);
Bitmap b = Bitmap.createBitmap(view.getDrawingCache());// *I am getting error here*
view.setDrawingCacheEnabled(false);
String extr = Environment.getExternalStorageDirectory().toString();
File myPath = new File(extr, "Schedule.png");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(myPath);
b.compress(Bitmap.CompressFormat.PNG, 20, fos);
fos.flush();
fos.close();
MediaStore.Images.Media.insertImage(getContentResolver(), b,
"Screen", "screen");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return myPath;
}
我遇到以下情况的错误:
1)TableLayout超出了屏幕尺寸。
当tablelayout完全在屏幕尺寸范围内时,我得到了完美的位图图像。
我在线搜索过,并尝试过CreateScaledBitmap ..还有其他选项......但都没有用。
Bitmap b = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(b);
Drawable bgDrawable =view.getBackground();
if (bgDrawable!=null)
bgDrawable.draw(canvas);
else
canvas.drawColor(Color.WHITE);
view.draw(canvas);