Tomar captura de pantalla con el diálogo

Necesito tomar captura de pantalla mediante programación de la vista de actividad. He encontrado un montón de respuestas cómo hacerlo, pero no hay respuesta a cómo hacerlo con diálogo abierto

Ok este es un poco complicado. No hay manera directa de hacerlo hasta donde yo sé. Aquí hay algo que funciona. Traté de tomar fotos individuales y capas.

Dialog dialog = // Dialog that is showing View mainView = getSupportActivity().getWindow().getDecorView(); mainView = getSupportActivity().getWindow().getDecorView() .findViewById(android.R.id.content); mainView.setDrawingCacheEnabled(true); // This is the bitmap for the main activity Bitmap bitmap = mainView.getDrawingCache(); View dialogView = dialog.getView(); int location[] = new int[2]; mainView.getLocationOnScreen(location); int location2[] = new int[2]; dialogView.getLocationOnScreen(location2); dialogView.setDrawingCacheEnabled(true); // This is the bitmap for the dialog view Bitmap bitmap2 = dialogView.getDrawingCache(); Canvas canvas = new Canvas(bitmap); // Need to draw the dialogView into the right position canvas.drawBitmap(bitmap2, location2[0] - location[0], location2[1] - location[1], new Paint()); String filename = // filename to save File myPath = new File(filename); FileOutputStream fos = null; try { fos = new FileOutputStream(myPath); bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos); fos.flush(); fos.close(); Trace.d("Twitter", "The file path is " + myPath); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } 

Acabo de probar esto y funcionó. Espero que esto ayude. Avísame si no funciona.

Pruebe esta biblioteca: https://github.com/jraska/Falcon . Toma capturas de pantalla de todas las ventanas de aplicación activas, incluyendo diálogos y puede resolver su problema.

Utilice el botón "captura de pantalla" en la ventana "Dispositivos" de DDMS con un dispositivo / emulador conectado. Esto le permite tomar capturas de pantalla de lo que esté en pantalla, incluso cuando un diálogo está abierto.

Introduzca aquí la descripción de la imagen

EDIT: cuando respondí a esta pregunta, no estaba claro que debía hacerse de forma programática y más tarde se editó para aclarar.

Tengo código para capturar la pantalla programáticamente ..

Espero que este código te ayude

 public class CaptureScreenShots extends Activity { LinearLayout L1; ImageView image; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.screen_shots); L1 = (LinearLayout) findViewById(R.id.LinearLayout01); Button but = (Button) findViewById(R.id.munchscreen); but.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { View v1 = L1.getRootView(); v1.setDrawingCacheEnabled(true); Bitmap bm = v1.getDrawingCache(); BitmapDrawable bitmapDrawable = new BitmapDrawable(bm); image = (ImageView) findViewById(R.id.screenshots); image.setBackgroundDrawable(bitmapDrawable); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.screen_shots, menu); return true; } 

}

A continuación se muestra el archivo de diseño

  <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/LinearLayout01" > <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/munch" android:id="@+id/munchscreen" /> <ImageView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/screenshots" android:contentDescription="@string/app_name" /> </LinearLayout> 
FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.