简体   繁体   中英

show alert dialog in screenshot

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM