Android通过代码截取屏幕截图

Pea*_*nut 16 sms android screenshot

这不应该是一个问题太难.我希望能够截取我的布局(视图)并通过短信发送.有人可以走我的步骤吗?

谢谢!

编辑:我想,它不一定是一个"截图",只要我们能以某种方式从视图中获取所有渲染像素.

Pea*_*nut 23

在网络上,我发现了一些我能够一起工作的代码片段.

这是一个运作良好的解决方案:

设置Root布局:

View content = findViewById(R.id.layoutroot);
content.setDrawingCacheEnabled(true);
Run Code Online (Sandbox Code Playgroud)

获取渲染视图的函数:

private void getScreen()
{
    View content = findViewById(R.id.layoutroot);
    Bitmap bitmap = content.getDrawingCache();
    File file = new File("/sdcard/test.png");
    try 
    {
        file.createNewFile();
        FileOutputStream ostream = new FileOutputStream(file);
        bitmap.compress(CompressFormat.PNG, 100, ostream);
        ostream.close();
    } 
    catch (Exception e) 
    {
        e.printStackTrace();
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 记得在你的AndroidManifest中添加`<uses-permission android:name ="android.permission.WRITE_EXTERNAL_STORAGE"/>`而不是硬编码`/ sdcard /`使用`文件文件=新文件(Environment.getExternalStorageDirectory()+"/ test.png");` (11认同)