How to take a screenshot of the current MapView?

I have two buttons and a mapView. I want to save the MapView view when I click one of the buttons.

Has anyone understood how I can do this?

+3
source share
2 answers

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

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 for the image to appear immediately in the Gallery, you must notify MediaScanner of the new image as follows:

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

The google map has a function for creating a snapshot, so use it here, for example, it returns a bitmap that you can set to look like its simple ..

mMapsattelite.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
                        @Override
                        public void onMapLoaded() {
                            // Make a snapshot when map done loading
                            mMapsattelite.snapshot(new GoogleMap.SnapshotReadyCallback() {
                                @Override
                                public void onSnapshotReady(Bitmap bitmap) {
                                    bitmapMapsattelite = null;
                                    bitmapMapsattelite = bitmap;

                                }

                            });
                        }
                    });
0
source

All Articles