1

I have to design an app in which on click of the neutral button, an image of the alert dialogue gets stored in the sdcard. So I decided to programatically take a screenshot and then use the BitmapDecodeRegion class to crop the image.

But when i take the screenshot, the alert dialogue does not appear in it since it's not attached to the window. How can I attach it to the window?

here is the code snippet:

public void btnClick(View v) {
    Log.d("", "logger button clicked");
    AlertDialog.Builder dialog = new AlertDialog.Builder(this);
    dialog.setTitle("This is a demo!");
    dialog.setMessage("Lets see if this works");
    dialog.setCancelable(false);
    dialog.setNeutralButton("Take Snap",
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                    Bitmap bitmap;
                    View v1 = findViewById(android.R.id.content)
                            .getRootView();
                    v1.setDrawingCacheEnabled(true);
                    bitmap = Bitmap.createBitmap(v1.getDrawingCache());
                    GlobalObj.screenImg = bitmap;
                    v1.setDrawingCacheEnabled(false);

                    Intent i = new Intent(MainActivity.this,
                            ViewActivity.class);
                    startActivity(i);

                }
            });

    AlertDialog newDialog = dialog.create();
    newDialog.show();

Kindly help me out.

user3217900
  • 35
  • 1
  • 9
  • check this link: http://stackoverflow.com/questions/20136121/android-how-to-take-screenshot-programatically – Priya Mar 12 '14 at 08:37

2 Answers2

1

Try out this library: https://github.com/jraska/Falcon. It takes screenshots of all active windows of application including dialogs and could figure out your problem.

Josef Raška
  • 1,291
  • 10
  • 8
1

In your code change this line:

View v1 = findViewById(android.R.id.content)
                        .getRootView();

to this:

View v1 = AlertDialog.class.cast(dialog).getWindow().getDecorView().getRootView();

It works for me and does not need rooted phone. However this captures only the alert dialog on a black background, without the underlying views from your activity. If you want to have everything (not that simple) you can try this answer from Aswin Rajendiran.

Community
  • 1
  • 1
Vladimir
  • 1,045
  • 1
  • 11
  • 23