【问题标题】:Activity with MapFragment snapshot带有 MapFragment 快照的活动
【发布时间】:2026-01-30 00:10:01
【问题描述】:

我有一个 Activity,它在屏幕的一半上包含一个 MapFragment V2,而在另一半有更多的视图,例如带有详细信息的 TextViews。

屏幕是这样的:

现在,我想拍摄整个活动的快照,包括 MapFragment,但是当我尝试这样做时,我确实得到了一张图像,但地图部分是空白的(全黑)!

This is 我用来截屏的代码,效果很好!

我确实知道拍摄 MapFragment 快照的选项,但它只拍摄地图区域而没有屏幕的其余部分。 像这样的:

GoogleMap.SnapshotReadyCallback callback = new GoogleMap.SnapshotReadyCallback() {

            @Override
            public void onSnapshotReady(Bitmap snapshot) {
                // TODO Auto-generated method stub
                FileOutputStream out = null;
                boolean status = true;
                try {
                    out = new FileOutputStream(sharedImage);
                    snapshot = Bitmap.createScaledBitmap(snapshot, findViewById(R.id.trip_map).getWidth(),
                            findViewById(R.id.trip_map).getHeight(), true);
                    status = snapshot.compress(Bitmap.CompressFormat.JPEG, 100, out);
                    Toast.makeText(TripDetailsActivity.this, "save status: " + status, Toast.LENGTH_SHORT).show();
                    snapshot.recycle();
                    snapshot = null;
                    System.gc();
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    try {
                        out.flush();
                        out.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }
                openShareImageDialog(sharedImage);
            }
        };
        mMap.snapshot(callback);

    }
    return true;
}

有没有人知道当那里有 MapFragment 时如何拍摄整个屏幕快照?

【问题讨论】:

  • AFAIK Google 使用 OpenGL 来渲染地图,并且无法抓取 OpenGL 视图的屏幕截图(GoogleMap 拍摄快照除外)。

标签: android android-activity snapshot mapfragment


【解决方案1】:

在我的情况下试试这个,它对我有用,我知道两者都是一样的,但尝试使用 LinearLayout 作为父级,将所有元素保持在一个父级布局中,并在需要时截屏:

 public void getDrawingBitmap() throws Exception{

 LinearLayout tabLayout = (LinearLayout)findViewById(R.id.screen_shot_layout);

    if (tabLayout != null) {

        Bitmap image = Bitmap.createBitmap(tabLayout.getWidth(),
                tabLayout.getHeight(), Config.ARGB_8888);
        Canvas b = new Canvas(image);
        tabLayout.draw(b);
        if (image!=null) {
            // here is your sceenshot
        }else{
            Toast.makeText(DashBord.this, "Unabel to take screen shot", Toast.LENGTH_LONG).show();
        }
    }



}

【讨论】:

  • 谢谢,我会检查一下。
  • 抱歉,这个解决方案对我不起作用。您确定将其与地图片段一起使用吗?你能分享你得到的图像吗?
【解决方案2】:

你可以像这样使用谷歌地图回调函数:

GoogleMap.SnapshotReadyCallback callback = new GoogleMap.SnapshotReadyCallback() {
            @Override
            public void onSnapshotReady(Bitmap snapshot) {
                // create bitmap screen capture
                Bitmap bitmap = snapshot;

                OutputStream fout = null;
                File imageFile = new File(filePath);

                try {
                    fout = new FileOutputStream(imageFile);
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fout);
                    fout.flush();
                    fout.close();

                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        };

现在使用您的 Google 地图片段调用:

map.snapshot(callback);

【讨论】:

    最近更新 更多