Android 如何拍摄当前地图视图的屏幕截图?
我有两个按钮和一个地图视图。我想在按下其中一个按钮时保存地图视图的视图Android 如何拍摄当前地图视图的屏幕截图?,android,android-mapview,Android,Android Mapview,我有两个按钮和一个地图视图。我想在按下其中一个按钮时保存地图视图的视图 有人知道我如何做到这一点吗?您必须在按钮中添加一个侦听器(您应该知道如何做到这一点),然后在该侦听器中执行以下操作: boolean enabled = mMapView.isDrawingCacheEnabled(); mMapView.setDrawingCacheEnabled(true); Bitmap bm = mMapView.getDrawingCache(); /* now you've got the b
有人知道我如何做到这一点吗?您必须在按钮中添加一个侦听器(您应该知道如何做到这一点),然后在该侦听器中执行以下操作:
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);
}
private void captureScreen(){
//将captureScreen()放入OSMDroid片段中
//最好在布局OSMDroid fragment.xml中放置一个按钮
view=view.findViewById(R.id.mapview);//使用OSMDroid片段调用
view.setDrawingCacheEnabled(true);
位图bmp=Bitmap.createBitmap(view.getDrawingCache());
//建立你的文件夹
File sd=新文件(Environment.getExternalStorageDirectory()+“/ArqueoMaquina”);
如果(!sd.exists()){
sd.mkdir();
}
File imagePath=新文件(sd,“mapa”+System.currentTimeMillis()+“.png”);
文件输出流;
试一试{
fos=新文件输出流(imagePath);
bmp.compress(Bitmap.CompressFormat.JPEG,100,fos);
Uri=Uri.fromFile(imagePath);//获取手机中保存的.png地图的文件路径
fos.flush();
fos.close();
}catch(filenotfounde异常){
Log.e(“GREC”,e.getMessage(),e);
}捕获(IOE异常){
Log.e(“GREC”,e.getMessage(),e);
}
这些是结果。我的屏幕应用程序带有OSMDroid片段和按钮“保存地图”:
我的map.png保存在手机文件夹中,仅显示OSMDroid片段:
我什么都不尝试,也不知道从哪里开始搜索。附带一个不起作用的实现。然后人们会帮助你。感谢所有的Venky,感谢太多的RivieraKid编辑我的帖子。我在这里太新手了,我必须学会很好地使用它。Andro Selva我最后一次承诺。对不起,这对V2 Google Mapv不起作用查看。它只是重新运行一个只有缩放按钮可见的空白屏幕。@Mannaz我知道,但我不知道如何使用V2。他们在那里使用曲面视图,到目前为止,我只发现无法将曲面视图的内容作为位图获取的信息。而且V2 API也没有提供任何实现方法。尽管我理解你很失望,否决票不是很公平,因为答案来自一个没有V2 API的时代。如果有人想要谷歌地图V2的屏幕截图,请使用osmdroid mapview实现SnapshotReadyCallback。