Recently I’ve been working on a little Android project and wanted to save thumbnail images of a map within the application. This post is just sharing how to do exactly that. Nothing too complicated.
public class MyMapActivity extends MapActivity { private MapView mapView; ... private Bitmap getMapImage() { /* Position map for output */ MapController mc = mapView.getController(); mc.setCenter(SOME_POINT); mc.setZoom(16); /* Capture drawing cache as bitmap */ mapView.setDrawingCacheEnabled(true); Bitmap bmp = Bitmap.createBitmap(mapView.getDrawingCache()); mapView.setDrawingCacheEnabled(false); return bmp; } private void saveMapImage() { String filename = "foo.png"; File f = new File(getExternalFilesDir(null), filename); FileOutputStream out = new FileOutputStream(f); Bitmap bmp = getMapImage(); bmp.compress(Bitmap.CompressFormat.PNG, 100, out); out.close(); } }
In the getMapImage method, we’re telling the map controller to move to a particular point (this may not matter to you, you may just want to take the image as it appears) and zooming in to show a sufficient level of detail. Then a Bitmap is created from the map view’s drawing cache. The saveMapImage method is just an example of how you may want to save an image to the application’s external file directory.