Googlemaps编码截图返回黑色图像 [英] Googlemaps Coded screenshot returns black image

查看:99
本文介绍了Googlemaps编码截图返回黑色图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在设置市场后,我试图抓取Googlemaps的屏幕截图,以便对图像进行分解并区分海洋。
但是我得到的返回结果是一个黑色的地图截图,它不包含这里的数据流是数据
我在manifest.xml中的API级别是15

  import android.content.Intent; 
导入android.graphics.Bitmap;
导入android.net.Uri;
导入android.os.Environment;
导入android.os.Handler;
导入android.support.v4.app.FragmentActivity;
导入android.os.Bundle;
导入android.view.View;

import com.google.android.gms.maps.CameraUpdateFactory;
导入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;
导入com.google.android.gms.maps.model.MarkerOptions;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Date;

公共类MapsActivity扩展FragmentActivity实现OnMapReadyCallback {

私有GoogleMap mMap;
GPSTracker gps;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
//获取SupportMapFragment并在地图准备好使用时得到通知。
SupportMapFragment mapFragment =(SupportMapFragment)getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}


/ **
*操作一次可用的地图。
*当地图准备好使用时,会触发此回调。
*这是我们可以添加标记或线条,添加听众或移动相机的位置。在这种情况下,
*我们只在澳大利亚悉尼附近添加一个标记。
*如果设备上未安装Google Play服务,系统会提示用户在SupportMapFragment中安装
* it。只有当用户安装了
* Google Play服务并返回到应用时,才会触发此方法。
* /
final Handler handler = new Handler();
@Override
public void onMapReady(GoogleMap googleMap){
mMap = googleMap;

//在悉尼添加标记并移动相机
gps =新GPSTracker(MapsActivity.this);
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();
LatLng sydney =新纬度(纬度,经度);
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(){
//这是一个可运行/处理程序组合

@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);

尝试{
//图像命名和路径以包含您为文件选择的sd卡附加名称
String mPath = Environment.getExternalStorageDirectory()。toString()+/ + now +.jpg;

//创建位图屏幕截图
查看v1 = getWindow()。getDecorView()。getRootView();
v1.setDrawingCacheEnabled(true);
位图位图= Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);

文件imageFile =新文件(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){
//文件处理或者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);



$ div $解析方案

你可以使用 GoogleMap.snapshot 方法拍摄地图快照( documentation

您需要实施 > GoogleMap.SnapshotReadyCallback 来管理拍摄的快照。 另外,根据至 the documentation


注意: I地图的法师不得传输到您的服务器,或以其他方式在应用程序之外使用。如果您需要将地图发送给其他应用程序或用户,请发送数据,以便他们为新用户重建地图而不是快照。



I am trying to take a screenshot of Googlemaps after I set a market so I can use image decomposition on it and differentiate land from the ocean. But the return result I get is a black screenshot of the maps it does not take the stream here is the data My API level on the manifest.xml is 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);
    }
}

解决方案

You can use the GoogleMap.snapshot method to take snapshots of the map (documentation)

You will need to implement a GoogleMap.SnapshotReadyCallback to manage the snapshot taken.

Also, take into account that, according to the documentation:

Note: Images of the map must not be transmitted to your servers, or otherwise used outside of the application. If you need to send a map to another application or user, send data that allows them to reconstruct the map for the new user instead of a snapshot.

这篇关于Googlemaps编码截图返回黑色图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆