问题描述
我想在用户从地点选择器活动中选择地点时拍摄地图快照。我正在将此图像存储到SD卡。但它质量很差。我不明白为什么?我还在这行代码中将图像质量设置为100: bitmap.compress(Bitmap.CompressFormat.PNG,100,out); 。
快照如下所示:
但是我想要一些更好的图像,比如下面的图片中只包含Google的文字: b
$ b
我想要在对话框中显示的地图中间图像。我遵循
处理图片和拍摄快照的代码如下:
//开放地点选择器活动。
protected void onActivityResult(int requestCode,int resultCode,Intent data){
if(requestCode == PLACE_PICKER_REQUEST
&& resultCode == Activity.RESULT_OK){
final Place place = PlacePicker.getPlace(this,data);
final CharSequence name = place.getName();
final CharSequence address = place.getAddress();
字符串归属=(String)place.getAttributions();
if(attributions == null){
attributions =;
}
// tv4.setText(place.getLatLng()。toString()+\\\
+ name +\\\
+ address +\\\
+ attributions);要获得latitide和经度。
tv4.setText(地址+\\\
+ attributions);
LatLngBounds selectedPlaceBounds = PlacePicker.getLatLngBounds(data);
//将相机移动到选定边界
CameraUpdate相机= CameraUpdateFactory.newLatLngBounds(selectedPlaceBounds,0);
mMap.moveCamera(相机);
//获取快照并实现快照就绪回调
mMap.snapshot(new GoogleMap.SnapshotReadyCallback(){
位图位图= null;
public void onSnapshotReady(位图快照){
//在此处理快照
位图=快照;
尝试{
FileOutputStream out = new FileOutputStream(/ mnt / sdcard / Download / 000p.png) ;
bitmap.compress(Bitmap.CompressFormat.PNG,100,out);
Toast.makeText(Main2Activity.this,dsfds,Toast.LENGTH_LONG).show();
} catch(Exception e){
Toast.makeText(Main2Activity.this,e.toString(),Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
});
} else {
super.onActivityResult(requestCode,resultCode,data);
}
}
为了达到这个目的,我应该改变什么?有没有其他方法可以得到这个快照?
感谢您的帮助!
不确定这一点,但可能是您拍摄快照时未完成 CameraUpdate 。
您可以试试这个:
布尔placePicked = false;
protected void onActivityResult(int requestCode,int resultCode,Intent data){
$ b $ if(requestCode == PLACE_PICKER_REQUEST&& resultCode == Activity.RESULT_OK){
placePicked = true;
//除了拍摄快照之外,其他的一切。
} else {
super.onActivityResult(requestCode,resultCode,data);
@Override
public void onCameraIdle(){
if(placePicked){
placePicked = false;
//拍快照...
}
}
onCameraIdle 是当CameraUpdate完成时调用的方法。
您的活动应该实现 GoogleMap.OnCameraIdleListener ,并添加到您的地图 mMap.setOnCameraIdleListener(this); 内部 onMapReady()。
另外,我假设你的地图已经加载。如果没有,你应该使用 onMapLoaded()来解释。
I want to take a snapshot of the map when user select place from place picker activity. I'm storing this image to the SD card. But it gives very poor quality. I can't understand why? I've also set image quality to 100 in this line of code: bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);.
The snapshot looks like:
But I want little bit bigger image of good quality like one in the following image, which only contains text "Google":
I want the middle image of map which is displayed in the dialogue. I'm following this answer.
My code of handling the image and taking snapshot is as following:
//opening place picker activity. protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == PLACE_PICKER_REQUEST && resultCode == Activity.RESULT_OK) { final Place place = PlacePicker.getPlace(this, data); final CharSequence name = place.getName(); final CharSequence address = place.getAddress(); String attributions = (String) place.getAttributions(); if (attributions == null) { attributions = ""; } // tv4.setText(place.getLatLng().toString()+"\n"+name+"\n"+address+"\n"+attributions); To get latitide and longitudes. tv4.setText(address+"\n"+attributions); LatLngBounds selectedPlaceBounds = PlacePicker.getLatLngBounds(data); // move camera to selected bounds CameraUpdate camera = CameraUpdateFactory.newLatLngBounds(selectedPlaceBounds,0); mMap.moveCamera(camera); // take snapshot and implement the snapshot ready callback mMap.snapshot(new GoogleMap.SnapshotReadyCallback() { Bitmap bitmap=null; public void onSnapshotReady(Bitmap snapshot) { // handle snapshot here bitmap = snapshot; try { FileOutputStream out = new FileOutputStream("/mnt/sdcard/Download/000p.png"); bitmap.compress(Bitmap.CompressFormat.PNG, 100, out); Toast.makeText(Main2Activity.this,"dsfds",Toast.LENGTH_LONG).show(); } catch (Exception e) { Toast.makeText(Main2Activity.this,e.toString(),Toast.LENGTH_SHORT).show(); e.printStackTrace(); } } }); } else { super.onActivityResult(requestCode, resultCode, data); } }
What should I change to achieve this? Is there any other way to get this snapshot?
Thanks for help!
Not sure of this, but it might be that the CameraUpdate is not done when you take the snapshot.
You could try this:
boolean placePicked = false; protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == PLACE_PICKER_REQUEST && resultCode == Activity.RESULT_OK) { placePicked = true; // Do everything else, except taking the snapshot. } else { super.onActivityResult(requestCode, resultCode, data); } } @Override public void onCameraIdle() { if (placePicked) { placePicked = false; // take snapshot... } }
onCameraIdle is the method called when there was a CameraUpdate and it is finished.
Your activity should implement GoogleMap.OnCameraIdleListener, and add to your map mMap.setOnCameraIdleListener(this); inside onMapReady().
Also, I'm assuming your map is already loaded. If not, you should account for that using onMapLoaded().
这篇关于地图活动的质量非常差和快照太小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!