I have a Google Map Android Application which I try to capture the screenshot of the whole view together with the map. The screenshot is taken but the Google Map is all black only with the Google logo. I use the following code:
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>
I also attach the code of the layout. The output screenshot is the following image
After trying Brijesh's code I get the following output