捕获GoogleMap Android API V2的屏幕截图

最终更新

function请求已由Google完成。 请看下面的答案。

原来的问题

使用Google Maps Android API的旧版本,我可以捕获谷歌地图的屏幕截图,通过社交媒体进行分享。 我使用下面的代码来捕获屏幕截图并将图像保存到一个文件,它工作得很好:

public String captureScreen() { String storageState = Environment.getExternalStorageState(); Log.d("StorageState", "Storage state is: " + storageState); // image naming and path to include sd card appending name you choose for file String mPath = this.getFilesDir().getAbsolutePath(); // create bitmap screen capture Bitmap bitmap; View v1 = this.mapView.getRootView(); v1.setDrawingCacheEnabled(true); bitmap = Bitmap.createBitmap(v1.getDrawingCache()); v1.setDrawingCacheEnabled(false); OutputStream fout = null; String filePath = System.currentTimeMillis() + ".jpeg"; try { fout = openFileOutput(filePath, MODE_WORLD_READABLE); // Write the string to the file bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fout); fout.flush(); fout.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block Log.d("ImageCapture", "FileNotFoundException"); Log.d("ImageCapture", e.getMessage()); filePath = ""; } catch (IOException e) { // TODO Auto-generated catch block Log.d("ImageCapture", "IOException"); Log.d("ImageCapture", e.getMessage()); filePath = ""; } return filePath; } 

然而,api的V2使用的新的GoogleMap对象没有像MapView那样的“getRootView()”方法。

我试图做到这一点:

  SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() .findFragmentById(R.id.basicMap); View v1 = mapFragment.getView(); 

但我得到的截图没有任何地图内容,如下所示: 空白的地图截图

有没有人想出了如何截取新的Google Maps Android API V2?

更新

我也试图通过这种方式获得rootView:

 View v1 = getWindow().getDecorView().getRootView(); 

这会产生一个包含屏幕顶部的操作栏的屏幕截图,但是地图仍然像我附加的屏幕截图一样空白。

更新

function请求已提交给Google。 如果这是您希望Google未来添加的function,请转到function请求明星: 向Google Maps API V2添加屏幕截图function

更新 – Google已添加快照方法** !:

已经完成了对Android Google Map API V2 OpenGL图层进行截图的方法的function请求。

要截图,只需执行以下界面:

 public abstract void onSnapshotReady (Bitmap snapshot) 

并致电:

public final void snapshot (GoogleMap.SnapshotReadyCallback callback)

截取屏幕截图的示例,然后展示标准的“图像共享”选项:

 public void captureScreen() { SnapshotReadyCallback callback = new SnapshotReadyCallback() { @Override public void onSnapshotReady(Bitmap snapshot) { // TODO Auto-generated method stub bitmap = snapshot; OutputStream fout = null; String filePath = System.currentTimeMillis() + ".jpeg"; try { fout = openFileOutput(filePath, MODE_WORLD_READABLE); // Write the string to the file bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fout); fout.flush(); fout.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block Log.d("ImageCapture", "FileNotFoundException"); Log.d("ImageCapture", e.getMessage()); filePath = ""; } catch (IOException e) { // TODO Auto-generated catch block Log.d("ImageCapture", "IOException"); Log.d("ImageCapture", e.getMessage()); filePath = ""; } openShareImageDialog(filePath); } }; mMap.snapshot(callback); } 

一旦图像完成捕捉,它将触发标准的“共享图像”对话框,以便用户可以select他们想分享的方式:

 public void openShareImageDialog(String filePath) { File file = this.getFileStreamPath(filePath); if(!filePath.equals("")) { final ContentValues values = new ContentValues(2); values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg"); values.put(MediaStore.Images.Media.DATA, file.getAbsolutePath()); final Uri contentUriFile = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); final Intent intent = new Intent(android.content.Intent.ACTION_SEND); intent.setType("image/jpeg"); intent.putExtra(android.content.Intent.EXTRA_STREAM, contentUriFile); startActivity(Intent.createChooser(intent, "Share Image")); } else { //This is a custom class I use to show dialogs...simply replace this with whatever you want to show an error message, Toast, etc. DialogUtilities.showOkDialogWithText(this, R.string.shareImageFailed); } } 

文档在这里

以下是通过示例捕获Google Map V2的屏幕快照的步骤

第1步。打开Android Sdk Manager (Window > Android Sdk Manager)然后Expand Extras现在update/install Google Play Services to Revision 10忽略此步骤,如果已经installed

在此处阅读说明https://developers.google.com/maps/documentation/android/releases#august_2013

第2步。 Restart Eclipse

第3步。 import com.google.android.gms.maps.GoogleMap.SnapshotReadyCallback;

第4步。使方法捕获/存储像下面的地图的屏幕/图像

 public void CaptureMapScreen() { SnapshotReadyCallback callback = new SnapshotReadyCallback() { Bitmap bitmap; @Override public void onSnapshotReady(Bitmap snapshot) { // TODO Auto-generated method stub bitmap = snapshot; try { FileOutputStream out = new FileOutputStream("/mnt/sdcard/" + "MyMapScreen" + System.currentTimeMillis() + ".png"); // above "/mnt ..... png" => is a storage path (where image will be stored) + name of image you can customize as per your Requirement bitmap.compress(Bitmap.CompressFormat.PNG, 90, out); } catch (Exception e) { e.printStackTrace(); } } }; myMap.snapshot(callback); // myMap is object of GoogleMap +> GoogleMap myMap; // which is initialized in onCreate() => // myMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map_pass_home_call)).getMap(); } 

第5步。现在调用这个CaptureMapScreen()方法来捕获图像

在我的情况下,我calling this method on Button click in my onCreate()这是正常工作

喜欢:

 Button btnCap = (Button) findViewById(R.id.btnTakeScreenshot); btnCap.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub try { CaptureMapScreen(); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } }); 

检查文件在这里和这里

编辑 :此答案不再有效 – Google地图Android API V2上的屏幕截图function请求已完成。 看到这个答案的例子 。

原始接受答复

由于使用OpenGL显示新的Android API v2地图,因此不可能创build屏幕截图。

由于最高票的答案不能与地图片段(我正在寻找什么)顶部的多段线和其他叠加层一起工作,我想分享这个解决scheme。

 public void captureScreen() { GoogleMap.SnapshotReadyCallback callback = new GoogleMap.SnapshotReadyCallback() { @Override public void onSnapshotReady(Bitmap snapshot) { try { getWindow().getDecorView().findViewById(android.R.id.content).setDrawingCacheEnabled(true); Bitmap backBitmap = getWindow().getDecorView().findViewById(android.R.id.content).getDrawingCache(); Bitmap bmOverlay = Bitmap.createBitmap( backBitmap.getWidth(), backBitmap.getHeight(), backBitmap.getConfig()); Canvas canvas = new Canvas(bmOverlay); canvas.drawBitmap(snapshot, new Matrix(), null); canvas.drawBitmap(backBitmap, 0, 0, null); OutputStream fout = null; String filePath = System.currentTimeMillis() + ".jpeg"; try { fout = openFileOutput(filePath, MODE_WORLD_READABLE); // Write the string to the file bmOverlay.compress(Bitmap.CompressFormat.JPEG, 90, fout); fout.flush(); fout.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block Log.d("ImageCapture", "FileNotFoundException"); Log.d("ImageCapture", e.getMessage()); filePath = ""; } catch (IOException e) { // TODO Auto-generated catch block Log.d("ImageCapture", "IOException"); Log.d("ImageCapture", e.getMessage()); filePath = ""; } openShareImageDialog(filePath); } catch (Exception e) { e.printStackTrace(); } } }; ; map.snapshot(callback); } 

Eclipse DDMS甚至可以捕获屏幕即谷歌地图V2。

如果你有“root”,尝试调用/ system / bin / screencap或/ system / bin / screenshot。 我了解到,从Eclipse如何实现Android的DDMS“屏幕截图”