我有两个按钮和一个 mapView。当我按下其中一个按钮时,我想保存 MapView 的视图。
有人知道我该怎么做吗?
您必须向按钮添加一个侦听器(您应该知道这是如何完成的)并在该侦听器中执行以下操作:
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
为了让图像立即显示在图库中,您必须通知 MediaScanner 新图像,如下所示:
if (success) {
MediaScannerClientProxy client = new MediaScannerClientProxy(file.getAbsolutePath(), "image/png");
MediaScannerConnection msc = new MediaScannerConnection(this, client);
client.mConnection = msc;
msc.connect();
}
谷歌地图有一个拍摄快照的功能,所以在这里使用这个是示例,它返回一个位图,您可以在图像视图上设置它的简单..
Bitmap bitmapMapsattelite ;
map.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
@Override
public void onMapLoaded() {
// Make a snapshot when map's done loading
map.snapshot(new GoogleMap.SnapshotReadyCallback() {
@Override
public void onSnapshotReady(Bitmap bitmap) {
bitmapMapsattelite = null;
/// make the bitmap null so everytime it //will fetch the new bitmap
bitmapMapsattelite = bitmap;
}
});
}
});
我以这种方式使用 OSMDroid 仅拍摄片段屏幕。
private void captureScreen() {
// put captureScreen() inside your OSMDroid fragment
// it's better put a button inside your layout OSMDroid fragment.xml
view = view.findViewById(R.id.mapview); //using OSMDroid fragment to call <org.osmdroid.views.MapView/>
view.setDrawingCacheEnabled(true);
Bitmap bmp = Bitmap.createBitmap(view.getDrawingCache());
//build your folder
File sd = new File(Environment.getExternalStorageDirectory() + "/ArqueoMaquina");
if (!sd.exists()) {
sd.mkdir();
}
File imagePath = new File(sd , "mapa" + System.currentTimeMillis() + ".png");
FileOutputStream fos;
try {
fos = new FileOutputStream(imagePath);
bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
Uri uri = Uri.fromFile(imagePath); //to take the file path to your .png map saved in your phone
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
Log.e("GREC", e.getMessage(), e);
} catch (IOException e) {
Log.e("GREC", e.getMessage(), e);
}
Osmdroid 6.1.0 中引入了所需的功能。对应的类是MapSnapshot。
对于问题中的用例,在按钮的单击回调中添加以下代码将起作用。
final MapSnapshot mapSnapshot = new MapSnapshot(new MapSnapshot.MapSnapshotable() {
@Override
public void callback(final MapSnapshot pMapSnapshot) {
if (pMapSnapshot.getStatus() != MapSnapshot.Status.CANVAS_OK) {
return;
}
final Bitmap bitmap = Bitmap.createBitmap(pMapSnapshot.getBitmap());
// Do something with the bitmap like save to file
}
}, MapSnapshot.INCLUDE_FLAG_UPTODATE, pMapView);
new Thread(mapSnapshot).start();
谷歌地图有一个名为 snapshot -> map.snapshot 的回调,它返回一个位图进行处理。在osmdroid(open street map android api)中,对应的方法是getDrawingCache方法->map.getDrawingCache