2013-04-18 28 views
4

我曾經看過可能每篇SO文章,都着眼於在Android上以編程方式捕獲屏幕(屏幕截圖,screendump),並且他們通常都會得到相同的答案。如何捕捉當前屏幕上的所有內容,包括對話框

問題在於它捕獲了您指定的視圖,但它不捕獲可能位於「根視圖」之上的任何對話框。這是我使用的代碼,即不能「上頭」捕獲任何:

Bitmap bitmap; 
View v1 = findViewById(android.R.id.content); 
v1.setDrawingCacheEnabled(true); 
bitmap = Bitmap.createBitmap(v1.getDrawingCache()); 
v1.setDrawingCacheEnabled(false); 
File path = Environment.getExternalStorageDirectory(); 
File file = new File(path, "myDump.jpg"); 

FileOutputStream outputStream; 

try 
{ 
    outputStream = new FileOutputStream(file); 
    bitmap.compress(Bitmap.CompressFormat.JPEG, 10, outputStream); 
    outputStream.flush(); 
    outputStream.close(); 
} 

catch (Exception e) 
{ 
    e.printStackTrace(); 
} 

的問題是:我怎樣才能捕捉整個屏幕,包括那些在上面的對話框?我只想捕捉我正在編寫的應用程序,而不是主屏幕或類似的東西,只是在我的根視圖之上的任何東西。

我確實讀了一些關於生根的內容,但我真的希望能夠完成應用程序的完整screendump即時編寫不是不可能的。

+0

「現在的問題是:我怎麼能捕捉整個屏幕,包括那些在上面的對話框?」 - AFAIK,你不能,除了通過根方法。 – CommonsWare

+0

如果對話框是你的,你應該也可以爲它們使用getDrawingCache()方法,那麼它只是由你來覆蓋activity.jpg頂部的dialog.jpg。 – FoamyGuy

+0

Thx ...所以,我必須「向後」迭代,從根目錄到子目錄,並將每個視圖疊加到根視圖圖像上? *嘆息* – Ted

回答

0

這是在打開的DialogFragment中工作的。

View v1 = ((ViewGroup) (((MyActivity)getActivity()).findViewById(android.R.id.content))); 
v1.setDrawingCacheEnabled(true); 
Bitmap bitmapParent = Bitmap.createBitmap(v1.getDrawingCache()); 
v1.setDrawingCacheEnabled(false); 

// dialogView is the inflated view of the DialogFragment 
dialogView.setDrawingCacheEnabled(true); 
Bitmap bitmapDialog = Bitmap.createBitmap(dialogView.getDrawingCache()); 
dialogView.setDrawingCacheEnabled(false); 

Canvas canvas = new Canvas(bitmapParent); 
Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG); 
canvas.drawBitmap(bitmapDialog, 0, 0, paint); 

// Activity and dialog captured!! 
bitmapParent.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(new File(directory, name))); 
0

使用這個庫....它對我很好。 https://github.com/jraska/Falcon

// Saving screenshot to file 
      Falcon.takeScreenshot(this, imageFile); 
      // Take bitmap and do whatever you want 
      Bitmap bitmap = Falcon.takeScreenshotBitmap(this); 
+0

請將鏈接的主要內容添加到問題主體。 – mhatch