如何以编程方式采取截图的提示对话框中的Andr​​oid [英] How To Programmatically take a screenshot in android of Alert Dialog

查看:289
本文介绍了如何以编程方式采取截图的提示对话框中的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了如下的<一个href=\"http://stackoverflow.com/questions/2661536/how-to-programmatically-take-a-screenshot-in-android\">Link它确实采取截图与顶级答案

I've seen the following Link and it does take a screenshot with the top answer

不过,我要的是为应用程序采取,我向用户显示警告对话框的截图,上面的解决方案,并低于code只需要目前是什么警告对话框后面的屏幕截图和因此,没有好

However, what I want is for the app to take a screenshot of the Alert Dialog that I am displaying to the user, the above solution and below code only takes a screenshot of what is currently behind the alert dialog and therefore no good

下面是万一有人正在使用没有通过所提供的链接

Here is the code being used in case anyone hasn't gone through the link provided

Date now = new Date();
    android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);

    try {
        // image naming and path  to include sd card  appending name you choose for file
        String mPath = Environment.getExternalStorageDirectory().toString() + "/" + now + ".jpg";

        // create bitmap screen capture
        View v1 = getWindow().getDecorView().getRootView();
        v1.setDrawingCacheEnabled(true);
        Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
        v1.setDrawingCacheEnabled(false);

        File imageFile = new File(mPath);

        FileOutputStream outputStream = new FileOutputStream(imageFile);
        int quality = 100;
        bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
        outputStream.flush();
        outputStream.close();

        openScreenshot(imageFile);
    } catch (Throwable e) {
        // Several error may come out with file handling or OOM
        e.printStackTrace();
    }

编辑:code的对话框,要求

code for dialog as requested

public void showCalc(String title, String message) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setCancelable(true);
    builder.setTitle(title);
    builder.setMessage(message);
    builder.setPositiveButton("Capture + Open",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    //Remove Values From Inventory
                    captureScreenAndOpen();

                }
            });

    builder.setNegativeButton("Capture",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    captureScreen();
                    Context context = getApplicationContext();
                    Toast.makeText(context, "Screenshot Captured", Toast.LENGTH_LONG).show();
                }
            });

    builder.setNeutralButton("Return", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
        }
    });
    builder.show();
}

进一步编辑:

在这里你会看到两个截图,第一个是显示当一切都被保存在从对话框中的截图保存的截图,你会在底部发现有一点文字始终是present在底部。

Here you will see two screenshots, the first is showing the saved screenshot when everything is saved in the screenshot from the dialog, you'll notice at the bottom there is a bit of text which is always present at the bottom.

Screenshot1

第二截图是那里是在对话框中这么多的文字对话是滚动,这样你可以看到所有的数据,你会发现,在第一张截图底部字符串不是present

The second screenshot is where there is so much text in the dialog the dialog is scrollable so that you can see all the data, you'll notice that the bottom string in the first screenshot is not present

Screenshot2

如果可能,我想它,所有的数据都显示,我不知道但如果一个截图功能将能够做到这一点还是一种替代方法

If possible I'd like it that all the data is displayed, I'm not sure though if a screenshot function would be able to do this or an alternative method

推荐答案

在Android模拟器5和工作开发。从您提供的链接,把你的对话框code和截图code。

Developed on Android 5 emulator and its working. Took Your dialog code and screenshot code from the link you have provided.

这是你的 AlertDialog

public void showCalc(String title, String message) {
    final AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setCancelable(true);
    builder.setTitle(title);
    builder.setMessage(message);
    builder.setPositiveButton("Capture + Open",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    //Remove Values From Inventory
                    captureScreenAndOpen();
                }
            });

    builder.setNegativeButton("Capture",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    AlertDialog dialog2 =AlertDialog.class.cast(dialog);
                    takeScreenshot(dialog2);
                    Context context = getApplicationContext();
                    Toast.makeText(context, "Screenshot Captured", Toast.LENGTH_LONG).show();
                }
            });

    builder.setNeutralButton("Return", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
        }
    });
    builder.show();
}

这是截图code

private void takeScreenshot(AlertDialog dialog) {
    Date now = new Date();
    android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);

    try {
        // image naming and path  to include sd card  appending name you choose for file
        String mPath = "/data/data/com.rohit.test/test.jpg"; // use your desired path

        // create bitmap screen capture
        View v1 = dialog.getWindow().getDecorView().getRootView();

        v1.setDrawingCacheEnabled(true);
        Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
        v1.setDrawingCacheEnabled(false);

        File imageFile = new File(mPath);

        FileOutputStream outputStream = new FileOutputStream(imageFile);
        int quality = 100;
        bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
        outputStream.flush();
        outputStream.close();

    } catch (Throwable e) {
        // Several error may come out with file handling or OOM
        e.printStackTrace();
    }
}

截图采取

在这里输入的形象描述

注1::您可以概括的方法 takeScreenshot 如果您更改参数类型为查看移动 dialog.getWindow()getDecorView()getRootView(); 从哪里这种方法被称为对话框code

Note1: You can generalize the method takeScreenshot if you change the argument type to View and move dialog.getWindow().getDecorView().getRootView(); to dialog code from where this method is called.

注2:看到你更新的问题。我不认为你可以得到整个数据截图时,他们中的一些是隐藏的。认为这是一个像一个正常的截屏(电脑,甚至手机上)。你拿的东西只有你能看到的画面。

Note2: Saw you updated question. I don't think you can get whole data in screenshot when some of them are hidden. Think it as like a normal screenshot (on computer or even phone). You take picture of only what you can see.

这篇关于如何以编程方式采取截图的提示对话框中的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆