0

我想在按钮单击时拍摄我的设备屏幕快照。请提供一些代码示例而不是链接。

提前致谢。

4

2 回答 2

4

我得到了我的问题的答案。实际上,我将位图设为空。但我找到了原因和解决方案。

View v = findViewById(R.id.attachments_list);
    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, v.getMeasuredWidth(), v.getMeasuredHeight());

    v.buildDrawingCache(true);
    Bitmap bitmap = Bitmap.createBitmap(v.getDrawingCache());
于 2012-10-18T10:46:41.817 回答
1

您无法获取设备的屏幕截图(未植根),但在您的应用程序中可以。

下面是截取应用程序屏幕并将文件保存在 sdcard 中的代码。

mLayoutRoot.setDrawingCacheEnabled(true); //mLayoutRoot is your Parent Layout(may be RelativeLayout, LinearLayout or etc..)
mLayoutRoot.buildDrawingCache();

Bitmap mBitmap= mLayoutRoot.getDrawingCache();
try {
    if(mBitmap!=null)
    {
        FileOutputStream out = new FileOutputStream("/sdcard/filename.png"));
        mBitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
        out.flush();
        out.close();
    }
} catch (Exception e) {}            
于 2012-10-18T08:54:48.303 回答