如何以编程方式在Alert对话框的android中截取屏幕截图

时间:2016-02-16 11:04:02

标签: android screenshot

我见过以下Link,它确实采用了最佳答案截图

然而,我想要的是应用程序截取我正在向用户显示的警报对话框的屏幕截图,上面的解决方案和下面的代码只截取警报对话框背后的内容,因此没有好处

以下是在没有人通过提供的链接时使用的代码

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();
    }

编辑:请求对话框的代码

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();
}

进一步编辑:

在这里你会看到两个截图,第一个显示保存的屏幕截图,当所有内容都保存在对话框的屏幕截图中时,你会注意到底部有一些文本总是出现在底部。< / p>

Screenshot1

第二个屏幕截图是对话框中有太多文本可以滚动对话框以便您可以看到所有数据,您会注意到第一个屏幕截图中的底部字符串不存在

Screenshot2

如果可能,我希望显示所有数据,我不确定截图功能是否可以执行此操作或替代方法

3 个答案:

答案 0 :(得分:5)

在Android 5模拟器上开发并运行。从您提供的链接中获取对话框代码和屏幕截图代码。

这是您的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();
}

这是截图代码

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();
    }
}

截图

enter image description here

注1:如果将参数类型更改为takeScreenshot并将View移动到调用此方法的对话框代码,则可以概括方法dialog.getWindow().getDecorView().getRootView();

注2:看到你更新了问题。我不认为你可以在屏幕截图中隐藏其中一些数据。将其视为普通屏幕截图(在计算机甚至手机上)。你只能看到你能看到的东西。

答案 1 :(得分:2)

试试这个图书馆:

https://github.com/jraska/Falcon

它可以捕获截图的对话框。

答案 2 :(得分:0)

<强> 1。我亲爱的朋友你做错了一件事你无法截取对话框。

#include <QSysInfo>
#ifdef Q_WS_MAC
switch(QSysInfo::MacintoshVersion())
{
  case QSysInfo::MV_IOS: return "IOS";//all IOS versions
  default: return "Windows";
}
#endif

您正在捕获AlertDialog

下面的整个屏幕

您可以使用这些方法通过将对话框的视图发送到此方法来完成工作

  

从视图中获取位图

function Test(value) {
    this.value = value

    this.action = function testAction() {
        console.log(this.value); // gives undefined!
    }

    this.play = function testPlay() {
        setInterval(this.action, 500);
    }
}

var test = new Test(20);
test.play();
  

保存位图

View v1 = getWindow().getDecorView().getRootView();