我不在这里使用xml,getroot()对我不起作用。我看到了Store Bitmap Screenshot in Android Cache,但我不知道他是如何得到mCurrentUrlMask参考的。
我的代码是:
View v= getRootView();
v.setDrawingCacheEnabled(true);
// this is the important code :)
// Without it the view will have a dimension of 0,0 and the bitmap will be null
v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
v.layout(0, 0, getMeasuredWidth(), getMeasuredHeight());
v.buildDrawingCache(true);
Bitmap bitmap =v.getDrawingCache();
path=Environment.getExternalStorageDirectory() + "/test.png";
File file = new File(path);
try
{
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(CompressFormat.PNG, 100, ostream);
ostream.close();
}
catch (Exception e)
{
e.printStackTrace();
}
我在使用模拟器时获得了一个ioexception权限并使用外部存储权限。(在使用xml和findviewbyid时,代码在测试应用程序上运行良好,但在此应用程序中我无法使用它。