谷歌地图Android API V2截图
最终更新 谷歌已经完成了这项功能要求。请看 原始问题 使用旧版本的谷歌地图Android API,我能够捕获谷歌地图的屏幕截图,并通过社交媒体共享。我使用以下代码捕获屏幕截图并将图像保存到文件中,效果非常好:谷歌地图Android API V2截图,android,android-mapview,google-maps-android-api-2,Android,Android Mapview,Google Maps Android Api 2,最终更新 谷歌已经完成了这项功能要求。请看 原始问题 使用旧版本的谷歌地图Android API,我能够捕获谷歌地图的屏幕截图,并通过社交媒体共享。我使用以下代码捕获屏幕截图并将图像保存到文件中,效果非常好: public String captureScreen() { String storageState = Environment.getExternalStorageState(); Log.d("StorageState", "Storage state is: " +
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();
但我得到的屏幕截图没有任何地图内容,如下所示:
有人知道如何拍摄新的谷歌地图Android API V2的截图吗
更新
我还尝试通过以下方式获取rootView:
View v1 = getWindow().getDecorView().getRootView();
这会产生一个屏幕截图,其中包括屏幕顶部的操作栏,但地图仍然是空白的,就像我附加的屏幕截图一样
更新
一个功能请求已经提交给谷歌。如果这是您希望google在将来添加的内容,请启动功能请求:编辑:此答案不再有效-google Maps Android API V2上的屏幕截图功能请求已完成。看 原始接受答案
由于新的Android API v2地图是使用OpenGL显示的,因此不可能创建屏幕截图。Eclipse DDMS可以捕获屏幕,即使它是google map v2
如果您有“root”,请尝试调用/system/bin/screencap或/system/bin/screenshot。我从更新中了解到,Google添加了一个快照方法**!: 已经完成了对Android Google Map API V2 OpenGL层截屏方法的功能请求 要获取屏幕截图,只需实现以下界面:
public abstract void onSnapshotReady (Bitmap snapshot)
并致电:
公共最终作废快照(GoogleMap.SnapshotReadyCallback回调)
该示例截图显示了标准的“图像共享”选项: 图像捕获完成后,它将触发标准的“共享图像”对话框,以便用户选择如何共享图像:
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管理器(窗口>Android Sdk管理器)
然后展开附加功能
now更新/安装Google Play Services至第10版
如果已安装则忽略此步骤
在这里读笔记
第2步。重新启动Eclipse
第3步。导入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()
方法来捕获图像
在我的例子中,在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();
}
}
});
gmap.setOnMapLoadedCallback(mapLoadedCallback);
final SnapshotReadyCallback snapReadyCallback = new SnapshotReadyCallback() {
Bitmap bitmap;
@Override
public void onSnapshotReady(Bitmap snapshot) {
bitmap = snapshot;
try {
//do something with your snapshot
imageview.setImageBitmap(bitmap);
} catch (Exception e) {
e.printStackTrace();
}
}
};
GoogleMap.OnMapLoadedCallback mapLoadedCallback = new GoogleMap.OnMapLoadedCallback() {
@Override
public void onMapLoaded() {
gmap.snapshot(snapReadyCallback);
}
};
检查Doc和,因为投票最多的答案不适用于地图片段顶部的多段线和其他覆盖(我一直在寻找),所以我想分享这个解决方案
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);
}
我希望这将有助于捕获您的地图截图
方法调用:
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();
}
}
});
gmap.setOnMapLoadedCallback(mapLoadedCallback);
final SnapshotReadyCallback snapReadyCallback = new SnapshotReadyCallback() {
Bitmap bitmap;
@Override
public void onSnapshotReady(Bitmap snapshot) {
bitmap = snapshot;
try {
//do something with your snapshot
imageview.setImageBitmap(bitmap);
} catch (Exception e) {
e.printStackTrace();
}
}
};
GoogleMap.OnMapLoadedCallback mapLoadedCallback = new GoogleMap.OnMapLoadedCallback() {
@Override
public void onMapLoaded() {
gmap.snapshot(snapReadyCallback);
}
};
方法声明:
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();
}
}
});
gmap.setOnMapLoadedCallback(mapLoadedCallback);
final SnapshotReadyCallback snapReadyCallback = new SnapshotReadyCallback() {
Bitmap bitmap;
@Override
public void onSnapshotReady(Bitmap snapshot) {
bitmap = snapshot;
try {
//do something with your snapshot
imageview.setImageBitmap(bitmap);
} catch (Exception e) {
e.printStackTrace();
}
}
};
GoogleMap.OnMapLoadedCallback mapLoadedCallback = new GoogleMap.OnMapLoadedCallback() {
@Override
public void onMapLoaded() {
gmap.snapshot(snapReadyCallback);
}
};
文件
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_maps);
linearLayout=(LinearLayout)findViewById (R.id.linearlayout);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
mapFragment = (SupportMapFragment)getSupportFragmentManager ()
.findFragmentById (R.id.map);
mapFragment.getMapAsync (this);
//Taking Snapshot of Google Map
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng (-26.888033, 75.802754);
mMap.addMarker (new MarkerOptions ().position (sydney).title ("Kailash Tower"));
mMap.moveCamera (CameraUpdateFactory.newLatLng (sydney));
mMap.setOnMapLoadedCallback (new GoogleMap.OnMapLoadedCallback () {
@Override
public void onMapLoaded() {
snapShot();
}
});
}
// Initializing Snapshot Method
public void snapShot(){
GoogleMap.SnapshotReadyCallback callback=new GoogleMap.SnapshotReadyCallback () {
Bitmap bitmap;
@Override
public void onSnapshotReady(Bitmap snapshot) {
bitmap=snapshot;
bitmap=getBitmapFromView(linearLayout);
try{
file=new File (getExternalCacheDir (),"map.png");
FileOutputStream fout=new FileOutputStream (file);
bitmap.compress (Bitmap.CompressFormat.PNG,90,fout);
Toast.makeText (MapsActivity.this, "Capture", Toast.LENGTH_SHORT).show ();
sendSceenShot (file);
}catch (Exception e){
e.printStackTrace ();
Toast.makeText (MapsActivity.this, "Not Capture", Toast.LENGTH_SHORT).show ();
}
}
};mMap.snapshot (callback);
}
private Bitmap getBitmapFromView(View view) {
Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas (returnedBitmap);
Drawable bgDrawable =view.getBackground();
if (bgDrawable!=null) {
//has background drawable, then draw it on the canvas
bgDrawable.draw(canvas);
} else{
//does not have background drawable, then draw white background on the canvas
canvas.drawColor(Color.WHITE);
}
view.draw(canvas);
return returnedBitmap;
}
//Implementing Api using Retrofit
private void sendSceenShot(File file) {
RequestBody job=null;
Gson gson = new GsonBuilder ()
.setLenient ()
.create ();
Retrofit retrofit = new Retrofit.Builder ()
.baseUrl (BaseUrl.url)
.addConverterFactory (GsonConverterFactory.create (gson))
.build ();
final RequestBody requestBody = RequestBody.create (MediaType.parse ("image/*"),file);
job=RequestBody.create (MediaType.parse ("text"),jobId);
MultipartBody.Part fileToUpload = MultipartBody.Part.createFormData ("name",file.getName (), requestBody);
API service = retrofit.create (API.class);
Call<ScreenCapture_Pojo> call=service.sendScreen (job,fileToUpload);
call.enqueue (new Callback<ScreenCapture_Pojo> () {
@Override
public void onResponse(Call <ScreenCapture_Pojo> call, Response<ScreenCapture_Pojo> response) {
if (response.body ().getMessage ().equalsIgnoreCase ("Success")){
Toast.makeText (MapsActivity.this, "success", Toast.LENGTH_SHORT).show ();
}
}
@Override
public void onFailure(Call <ScreenCapture_Pojo> call, Throwable t) {
}
});
}
@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_映射);
linearLayout=(linearLayout)findViewById(R.id.linearLayout);
//获取SupportMapFragment,并在地图准备好使用时收到通知。
mapFragment=(SupportMapFragment)getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(此文件);
//拍摄谷歌地图快照
}
/**
*一旦可用,就可以操纵贴图。
*当映射准备好使用时,将触发此回调。
*这是我们可以添加标记或线条、添加侦听器或移动摄影机的地方。在这种情况下,,
*我们只是在澳大利亚悉尼附近加了一个标记。
*如果设备上未安装Google Play服务,系统将提示用户安装
*它位于SupportMapFragment中。此方法仅在用户完成后触发
*已安装Google Play服务并返回应用程序。
*/
@凌驾
4月1日公开作废(谷歌地图谷歌地图){
mMap=谷歌地图;
//在Sydney添加一个标记并移动相机
悉尼LatLng=新LatLng(-26.888033,75.802754);
mMap.addMarker(新MarkerOptions().位置(悉尼)。标题(“凯拉什塔”);
mMap.moveCamera(CameraUpdateFactory.newLatLng(悉尼));
mMap.setOnMapLoadedCallback(新的GoogleMap.OnMapLoadedCallback(){
@凌驾
加载时的公共无效(){
快照();
}
});
}
//初始化快照方法
公共无效快照(){
GoogleMap.SnapshotReadyCallback回调=新建GoogleMap.SnapshotReadyCallback(){
位图;
@凌驾
公共无效onSnapshotReady(位图快照){
位图=快照;
位图=getBitmapFromView(线性布局);
试一试{
file=新文件(getExternalCacheDir(),“map.png”);
FileOutputStream fout=新的FileOutputStream(文件);
bitmap.compress(bitmap.CompressFormat.PNG,90,fout);
Toast.makeText(MapsActivity.this,“Capture”,Toast.LENGTH\u SHORT.show();
sendSceenShot(文件);
}捕获(例外e){
e、 printStackTrace();
Toast.makeText(MapsActivity.this,“非捕获”,Toast.LENGTH\u SHORT.show();
}
}
};mMap.snapshot(回调);
}
私有位图getBitmapFromView(视图){
一点