Final Update
The feature request has been fulfilled by Google. Please see this answer below.
Original Question
Using the old version of the Google Maps Android API, I was able to capture a screenshot of the google map to share via social media. I used the following code to capture the screenshot and save the image to a file and it worked great:
public String captureScreen()
{
String storageState = Environment.getExternalStorageState();
Log.d("StorageState", "Storage state is: " + storageState);
// image naming and path to include sd card appending name you choose for file
String mPath = this.getFilesDir().getAbsolutePath();
// create bitmap screen capture
Bitmap bitmap;
View v1 = this.mapView.getRootView();
v1.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
OutputStream fout = null;
String filePath = System.currentTimeMillis() + ".jpeg";
try
{
fout = openFileOutput(filePath,
MODE_WORLD_READABLE);
// Write the string to the file
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fout);
fout.flush();
fout.close();
}
catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
Log.d("ImageCapture", "FileNotFoundException");
Log.d("ImageCapture", e.getMessage());
filePath = "";
}
catch (IOException e)
{
// TODO Auto-generated catch block
Log.d("ImageCapture", "IOException");
Log.d("ImageCapture", e.getMessage());
filePath = "";
}
return filePath;
}
However, the new GoogleMap object used by V2 of the api does not have a "getRootView()" method like MapView does.
I tried to do this:
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.basicMap);
View v1 = mapFragment.getView();
But the screenshot that I get does not have any map content and looks like this:
Has anyone figured out how to take a screenshot of the new Google Maps Android API V2?
Update
I also tried to get the rootView this way:
View v1 = getWindow().getDecorView().getRootView();
This results in a screenshot that includes the action bar at the top of the screen, but the map is still blank like the screenshot I attached.
Update
A feature request has been submitted to Google. Please go star the feature request if this is something you want google to add in the future: Add screenshot ability to Google Maps API V2
I hope this would help to capture the screenshot of your map
Method call:
Method declaration:
Below are the steps to capture screen shot of Google Map V2 with example
Step 1. open
Android Sdk Manager (Window > Android Sdk Manager)
thenExpand Extras
nowupdate/install Google Play Services to Revision 10
ignore this step if alreadyinstalled
Read Notes here https://developers.google.com/maps/documentation/android/releases#august_2013
Step 2.
Restart Eclipse
Step 3.
import com.google.android.gms.maps.GoogleMap.SnapshotReadyCallback;
Step 4. Make Method to Capture/Store Screen/image of Map like below
Step 5. Now call this
CaptureMapScreen()
method where you want to capture the imagein my case i am
calling this method on Button click in my onCreate()
which is working finelike:
Check Doc here and here
Since the top voted answer doesnt work with polylines and other overlays on top of the map fragment (What I was looking for), I want to share this solution.
I capctured Map screenshot.It will be helpful
`
`
snapShot() method for taking screenshot of map
My output is below
Update - Google has added a snapshot method**!:
The feature request for a method to take a screen shot of the Android Google Map API V2 OpenGL layer has been fulfilled.
To take a screenshot, simply implement the following interface:
and call:
public final void snapshot (GoogleMap.SnapshotReadyCallback callback)
Example that takes a screenshot, then presents the standard "Image Sharing" options:
Once the image is finished being captured, it will trigger the standard "Share Image" dialog so the user can pick how they'd like to share it:
Documentation is here
Eclipse DDMS can capture the screen even it's google map V2.
Try to call /system/bin/screencap or /system/bin/screenshot if you have the "root". I learned that from How Eclipse android DDMS implement "screen capture"