2013-02-08 93 views
1

我知道,並非所有的Android設備都可以拍攝屏幕截圖,但我希望能夠以編程方式拍攝我的用戶遊戲屏幕的屏幕截圖,以便它們可以與朋友等有點像爬坡賽在那裏的比賽。如何以編程方式拍攝活動(遊戲)的屏幕截圖

有誰知道如何做到這一點?我嘗試了幾個解決方案,發佈在這裏:

// 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 = activity.getCurrentFocus().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(); 
} 

但沒有運氣。任何其他方式來做到這一點?也許是一種新方法。

編輯:

我正在逐漸線查看V1空指針= activity.getCurrentFocus()getRootView();

+0

是否有任何錯誤消息? stacktraces或logcat也許? – ariefbayu 2013-02-08 14:17:19

+0

「但是沒有運氣」對你的症狀沒有用處。 – CommonsWare 2013-02-08 14:17:53

+0

剛剛更新了我的問題。對於混亂的傢伙很抱歉。 – 2013-02-08 14:35:15

回答

0

使用

查看V1 = getWindow()。getDecorView()。getRootView (); 。

代替

視圖V1 = activity.getCurrentFocus()getRootView();

它不會拋出空指針異常。

享受!