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

Sjh*_*son 6 android screenshot

我已经看到了以下链接,它确实采用了最佳答案截图

不过,我要的是为应用程序采取的警告对话框中的截图,我显示给用户,上述解决方案,下面的代码只需要目前是什么警告对话框后面,因此没有良好的截图

这是在没有人通过提供的链接的情况下使用的代码

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();
    }
Run Code Online (Sandbox Code Playgroud)

编辑:请求的对话框代码

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();
}
Run Code Online (Sandbox Code Playgroud)

进一步编辑:

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

Screenshot1

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

Screenshot2

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

Roh*_*5k2 6

在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();
}
Run Code Online (Sandbox Code Playgroud)

这是截图代码

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

截图

在此输入图像描述

注意1:takeScreenshot如果将参数类型更改为Viewdialog.getWindow().getDecorView().getRootView();从此处调用此方法的对话框代码,则可以概括该方法.

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