How to take a screenshot of the current MapView?

android, android-mapview

Solution

You have to add a listener to the button (you should know how this done) and in that listener, do something like that:

boolean enabled = mMapView.isDrawingCacheEnabled();
mMapView.setDrawingCacheEnabled(true);
Bitmap bm = mMapView.getDrawingCache();

/* now you've got the bitmap - go save it */

File path = Environment.getExternalStorageDirectory();
path = new File(path, "Pictures");
path.mkdirs();  // make sure the Pictures folder exists.
File file = new File(path, "filename.png");
BufferedOutputStream outStream = new BufferedOutputStream(new FileOutputStream(file));
boolean success = bm.compress(CompressFormat.PNG, 100, outStream);
outStream.flush();
outStream.close();

mMapView.setDrawingCacheEnabled(enabled);   // reset to original value

In order to have the image show up in the Gallery immediately, you have to notify the MediaScanner about the new image like so:

if (success) {
    MediaScannerClientProxy client = new MediaScannerClientProxy(file.getAbsolutePath(), "image/png");
    MediaScannerConnection msc = new MediaScannerConnection(this, client);
    client.mConnection = msc;
    msc.connect();
}

Problem

I have two buttons and a mapView. I want to save the MapView's view when I press one of the buttons. Anyone got any idea about how I can do this?

Original source