2013-04-18 126 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