获取当前活动(Android)中可见的任何AlertDialog实例的查看对象



我有代码,该代码将在当前窗口的root View的绘图屏幕截图。

问题是,屏幕截图仅用于正在运行的活动。它不会绘制活动中可能可见的任何警报。

有没有一种方法可以获取当前活动的屏幕以及任何其他可见的alertdialogs/views?

当前代码:

 View v1 = getWindow().getDecorView().getRootView();
 v1.setDrawingCacheEnabled(true);
 Bitmap bitmap = Bitmap.createScaledBitmap(v1.getDrawingCache(), 360, 740, false);
 v1.setDrawingCacheEnabled(false);

实现这样的屏幕截图方法 -

将您的AlertDialog传递到Capture((方法 -

private void capture(AlertDialog dialog) {
    Date time = new Date();
    android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", time);
    try {
        // image path
        String mPath = "/data/data/com.example.sample/" + time + ".jpg"; // 
        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) {
        e.printStackTrace();
    }
}

您可以通过将视图传递给此方法并根据您的要求更改视图V1来为任何其他视图创建屏幕截图。

相关内容

最新更新