首页 > 解决方案 > 应用截图?

问题描述

我有一个 Google Map Android 应用程序,我尝试将整个视图的屏幕截图与地图一起捕获。截取了屏幕截图,但谷歌地图全黑,只有谷歌标志。我使用以下代码:

private void captureScreen() {
    View v = getWindow().getDecorView().getRootView();
    v.setDrawingCacheEnabled(true);
    Bitmap bmp = Bitmap.createBitmap(v.getDrawingCache());
    v.setDrawingCacheEnabled(false);
    try {
        FileOutputStream fos = new FileOutputStream(new File(Environment
                .getExternalStorageDirectory().toString(), "SCREEN"
                + System.currentTimeMillis() + ".png"));
        bmp.compress(Bitmap.CompressFormat.PNG, 100, fos);
        fos.flush();
        fos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/mapLayout">

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
tools:context="net.aratos.adc.MapActivity"
android:layout_height="0dp"
android:layout_weight="0.8"/>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:layout_weight="0.1"
    android:layout_gravity="center_horizontal">

    <Button
        android:gravity="center"
        android:id="@+id/loadDateButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Load date" />

    <Button
        android:gravity="center"
        android:id="@+id/snapshotButton"
        android:text="Snapshot"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:gravity="center"
        android:id="@+id/exportButton"
        android:text="Export PDF"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

<GridView
    android:orientation="horizontal"
    android:id="@+id/boundsLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:numColumns="auto_fit">

</GridView>
</LinearLayout>

我还附上了布局的代码。输出截图如下

尝试 Brijesh 的代码后,我得到以下输出

标签: androidgoogle-mapsscreenshot

解决方案


您可以使用 GoogleMap.snapshot 方法拍摄地图的快照(文档

您将需要实施一个

GoogleMap.SnapshotReadyCallback

管理拍摄的快照。

另外,请考虑到,根据文档:

注意: 地图的图像不得传输到您的服务器,或以其他方式在应用程序之外使用。如果您需要将地图发送给另一个应用程序或用户,请发送允许他们为新用户重建地图而不是快照的数据。


推荐阅读