Dis*_*Dev 47 android android-mapview google-maps-android-api-2
最后更新
原始问题
使用旧版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;
}
Run Code Online (Sandbox Code Playgroud)
但是,api的V2使用的新GoogleMap对象没有像MapView那样的"getRootView()"方法.
我试着这样做:
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.basicMap);
View v1 = mapFragment.getView();
Run Code Online (Sandbox Code Playgroud)
但我得到的截图没有任何地图内容,看起来像这样:

有没有人想出如何截取新的谷歌地图Android API V2的截图?
更新
我也尝试以这种方式获取rootView:
View v1 = getWindow().getDecorView().getRootView();
Run Code Online (Sandbox Code Playgroud)
这会产生一个屏幕截图,其中包含屏幕顶部的操作栏,但地图仍然是空白的,就像我附上的屏幕截图一样.
更新
功能请求已提交给Google.如果这是您希望将来谷歌添加的内容,请点击功能请求: 为Google Maps API V2添加屏幕截图功能
Dis*_*Dev 64
更新 - 谷歌添加了快照方法**!:
已完成针对Android Google Map API V2 OpenGL图层的屏幕截图的方法的功能请求.
要截取屏幕截图,只需实现以下界面:
public abstract void onSnapshotReady (Bitmap snapshot)
Run Code Online (Sandbox Code Playgroud)
并致电:
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);
}
Run Code Online (Sandbox Code Playgroud)
一旦图像完成捕获,它将触发标准的"共享图像"对话框,以便用户可以选择他们想要共享的方式:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
文档在这里
Tar*_*ngh 30
以下是捕获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();
}
Run Code Online (Sandbox Code Playgroud)
步骤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();
}
}
});
Run Code Online (Sandbox Code Playgroud)
小智 5
我捕获了地图截图。它会有所帮助
private GoogleMap map;
private static LatLng latLong;
Run Code Online (Sandbox Code Playgroud)
`
public void onMapReady(GoogleMap googleMap) {
map = googleMap;
setMap(this.map);
animateCamera();
map.moveCamera (CameraUpdateFactory.newLatLng (latLong));
map.setOnMapLoadedCallback (new GoogleMap.OnMapLoadedCallback () {
@Override
public void onMapLoaded() {
snapShot();
}
});
}
Run Code Online (Sandbox Code Playgroud)
`
snapShot() 用于截取地图截图的方法
public void snapShot(){
GoogleMap.SnapshotReadyCallback callback=new GoogleMap.SnapshotReadyCallback () {
Bitmap bitmap;
@Override
public void onSnapshotReady(Bitmap snapshot) {
bitmap=snapshot;
try{
file=new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"map.png");
FileOutputStream fout=new FileOutputStream (file);
bitmap.compress (Bitmap.CompressFormat.PNG,90,fout);
Toast.makeText (PastValuations.this, "Capture", Toast.LENGTH_SHORT).show ();
}catch (Exception e){
e.printStackTrace ();
Toast.makeText (PastValuations.this, "Not Capture", Toast.LENGTH_SHORT).show ();
}
}
};map.snapshot (callback);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
38546 次 |
| 最近记录: |