2012-10-18 71 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

顯然是親愛的;) –

相關問題