2011-05-09 78 views
16

这不应该是一个太强硬的问题。我希望能够截取我的布局(视图)并通过短信发送。有人能通过步骤走路吗?Android通过代码截图

谢谢!

编辑: 我猜,它不一定是'截图',只要我们可以以某种方式从视图中获取所有渲染像素即可。

+0

你需要运行搜索:

View content = findViewById(R.id.layoutroot); content.setDrawingCacheEnabled(true); 

函数来获取渲染视图题。这里几乎确切的愚蠢http://goo.gl/K9ezs – Bostone 2011-05-09 16:54:45

+0

可能重复的[Android屏幕截图并保存到SD卡](http://stackoverflow.com/questions/5929403/take-screenshot-of-android-屏幕保存到SD卡) – MByD 2011-05-09 16:55:01

回答

23

在网络上,我发现了一些代码片段,我可以一起工作。

这里是一个行之有效的解决方案:

设置你的根布局:询问之前

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(); 
    } 
} 
+11

记得在你的AndroidManifest中添加''而不是硬编码'/ sdcard /'use'File file = new File(Environment.getExternalStorageDirectory()+“/test.png”);' – Macarse 2011-06-07 12:22:46

+1

@Pananut我使用了这个解决方案。 test.png是在SD卡中创建的。但实际上图像不创建它显示0kb。我得到空指针异常在这里“bitmap.compress(CompressFormat.PNG,100,ostream);” – vnshetty 2011-07-21 04:16:05

+1

在这里得到了解决方案谢谢.. http://stackoverflow.com/questions/2339429/android-view-getdrawingcache-returns-null-only-null – vnshetty 2011-07-21 05:13:54