2012-10-18 72 views
0

我想在Button按钮上点击设备屏幕快照的快照。请提供一些代码示例而不是链接。如何以编程方式拍摄设备屏幕快照?

在此先感谢。

+0

您的手机是否已经植根? – didster

+0

在应用程序或出应用程序? –

+0

你只能截取你的应用程序的截图,否则这将是Android –

回答

4

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

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

还没有。其实我读过的地方,每个网站都有自己的机制,提供像youtube提供的缩略图。但是对于视频Url来说,并没有一条硬性规定。另一件事是我们可以为保存在手机中或下载的视频制作缩略图,但对于流式视频没有具体的方法。 –

+0

嘿@venkat我得到了解决方案 –

+0

http://api.embed.ly/1/oembed?url=PASTE YOUR URL&maxwidth = 500 –

1

你不能把你的设备的截图(没有根),但你可以在你的应用程序。

下面是代码,其中应用程序的屏幕截图并保存在您的SD卡中的文件。

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) {}    
+0

获取mLayoutRoot.getDrawingCache()为null :( –

+0

您是否使用findViewById(R.id.parentid)初始化了'mLayoutRoot';?*再次检查* –

+0

显然是亲爱的;) –

相关问题