当用户从地点选择器活动中选择地点时,我想拍摄地图的快照。我将此图像存储到SD卡中。但它的质量很差。我不明白为什么?我还在这行代码中将图像质量设置为100:bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
。
快照如下所示:
但是我想要更好的高质量图像,如下图所示,只包含文字" Google":
我想要对话中显示的地图的中间图像。我跟随this answer.
我处理图像和拍摄快照的代码如下:
//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);
}
}
为了达到这个目的,我应该改变什么?有没有其他方法来获取此快照?
感谢您的帮助!
答案 0 :(得分:0)
不确定这一点,但拍摄快照时可能没有完成CameraUpdate
。
你可以试试这个:
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
是在有CameraUpdate时调用的方法,它已完成。
您的活动应实施GoogleMap.OnCameraIdleListener
,并在mMap.setOnCameraIdleListener(this);
内添加到您的地图onMapReady()
。
另外,我假设你的地图已经加载了。如果没有,您应该使用onMapLoaded()
来说明问题。