android如何在surfaceview中以编程方式拍摄屏幕截图

时间:2011-08-08 06:03:13

标签: android view

我不在这里使用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时,代码在测试应用程序上运行良好,但在此应用程序中我无法使用它。

0 个答案:

没有答案
相关问题