如何在Android程序中以编程方式截取整个屏幕的截图?

3

我需要在Android上截取整个屏幕的截图,我已经搜索了很多但他们都在谈论如何截取指定视图的截图,那么我该如何截取整个屏幕的截图呢?

我的意思是,通过程序实现。(而不是通过DDMS)


2
这取决于您使用的手机。在一些手机上,您可以同时按住音量减小按钮和电源按钮来截屏,截屏将存储在手机内的截图文件夹中。我建议通过USB将设备连接到系统上。通过Eclipse-->DDMS,您可以对移动设备上的当前屏幕进行截屏。 - khubaib
我已经编辑了我的答案,请查看如何在Android上以编程方式截屏的链接。 - Vamshi
6个回答

1

有一个可用于通过设备进行快照的库,它被称为ASL(Android截图库)

这里查看完整的源代码


1

在Eclipse中,进入DDMS视图并选择您的设备。然后点击屏幕截图(相机图标)按钮。

查看链接,它可能对您有帮助...


0

这段代码返回布局可见和不可见部分的屏幕截图。

private Bitmap getScreenBitmap() {
    View v = getWindow().getDecorView().findViewById(android.R.id.content);
    v.setDrawingCacheEnabled(true);
    int measureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    v.measure(measureSpec, measureSpec);
    width = v.getMeasuredWidth();
    height = v.getMeasuredHeight();

    v.layout(0, 0, width, height);
    v.buildDrawingCache(true);

    //Bitmap b = Bitmap.createBitmap(v.getDrawingCache());

    Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(b);
    v.draw(canvas);
    v.setDrawingCacheEnabled(false);
    return b;
}

0

尝试以下代码:

// image naming and path  to include sd card  appending name you choose for file
String mPath = Environment.getExternalStorageDirectory().toString() + "/" + ACCUWX.IMAGE_APPEND;   

// create bitmap screen capture
Bitmap bitmap;
View v1 = mCurrentUrlMask.getRootView();
v1.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);

OutputStream fout = null;
imageFile = new File(mPath);

try {
fout = new FileOutputStream(imageFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fout);
fout.flush();
fout.close();

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

请参考此答案


@Matt_9.0,mCurrentUrlMask 是什么? - Noman
@Noman我发了一个链接,这是我实际参考答案的来源。评论中有一个关于同样问题的讨论,最后一个用户的评论是:“我使用的是View v1 = getWindow().getDecorView().getRootView();而不是View v1 = mCurrentUrlMask.getRootView();并且它对我起作用。” 所以我认为这可能有效。 - user3513843
这种方法似乎只能捕获活动布局,而不能捕获诸如状态栏内容或任何覆盖层(例如警报)之类的东西。 - David Berry

0
你需要对设备进行Root,否则它无法工作。还要使您的应用程序获得超级用户访问权限。只需实现此代码,您就可以开始了:
public void screenShot() throws InterruptedException
{
    Process sh;
    try
    {
        sh = Runtime.getRuntime().exec("su", null, null);
        OutputStream os = sh.getOutputStream();
        os.write(("/system/bin/screencap -p " + "/sdcard/Image.png").getBytes("ASCII"));
        os.flush();
        os.close();
        sh.waitFor();
    }
    catch (IOException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

-5
在Eclipse中,转到“窗口”->“显示视图”->“其他”->“设备”。
选择您的设备,然后只需单击“相机图片”即可:

enter image description here


1
请阅读所提出的问题..!! - Noman

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接