我在设置市场后试图截取Googlemaps的屏幕截图,以便我可以对其进行图像分解,并将土地与海洋区分开来。 但是我得到的返回结果是地图的黑色截图,它不采取流这里的数据 我在manifest.xml上的API级别为15
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;
import android.os.Handler;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.view.View;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Date;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
GPSTracker gps;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
/**
* 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.
*/
final Handler handler = new Handler();
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
gps = new GPSTracker(MapsActivity.this);
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();
LatLng sydney = new LatLng(latitude, longitude);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Cyprus"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney) );
mMap.animateCamera( CameraUpdateFactory.zoomTo( 19.0f ) );
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
//Here's a runnable/handler combo
@Override
public void onMapClick(LatLng point) {
MarkerOptions marker = new MarkerOptions().position(
new LatLng(point.latitude, point.longitude)).title("New Marker");
mMap.addMarker(marker);
System.out.println(point.latitude+"---"+ point.longitude);
}
});
takeScreenshot();
}
private void takeScreenshot() {
Date now = new Date();
android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);
try {
// image naming and path to include sd card appending name you choose for file
String mPath = Environment.getExternalStorageDirectory().toString() + "/" + now + ".jpg";
// create bitmap screen capture
View v1 = getWindow().getDecorView().getRootView();
v1.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
File imageFile = new File(mPath);
FileOutputStream outputStream = new FileOutputStream(imageFile);
int quality = 100;
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
outputStream.flush();
outputStream.close();
openScreenshot(imageFile);
} catch (Throwable e) {
// Several error may come out with file handling or OOM
e.printStackTrace();
}
}
private void openScreenshot(File imageFile) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(imageFile);
intent.setDataAndType(uri, "image/*");
startActivity(intent);
}
}
答案 0 :(得分:1)
您可以使用GoogleMap.snapshot
方法拍摄地图的快照(documentation)
您需要实施GoogleMap.SnapshotReadyCallback
来管理拍摄的快照。
另外,根据the documentation:
考虑到这一点注意:地图图片不得传输到您的服务器,也不得在应用程序之外使用。如果您需要将地图发送给其他应用程序或用户,请发送允许他们为新用户而不是快照重建地图的数据。