在地图完成加载时,获取屏幕的自动屏幕截图

时间:2017-09-07 09:16:10

标签: android screenshot openstreetmap

我正在创建一个应用程序,用于在地图中显示用户的当前位置。我正在使用OpenStreetMap。这是我的代码:

public class MainActivity extends Activity {

private LocationManager locationManager;
private Location onlyOneLocation;
private final int REQUEST_FINE_LOCATION = 1234;
public GeoPoint startPoint;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Context ctx = getApplicationContext();

    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)
        ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_FINE_LOCATION);

    //important! set your user agent to prevent getting banned from the osm servers
    Configuration.getInstance().load(ctx, PreferenceManager.getDefaultSharedPreferences(ctx));
    setContentView(R.layout.activity_main);

    final MapView map = (MapView) findViewById(R.id.map);
    map.setTileSource(TileSourceFactory.MAPNIK);
    map.setBuiltInZoomControls(true);
    map.setMultiTouchControls(true);

    final IMapController mapController = map.getController();
    mapController.setZoom(18);

    CurrentLocation.requestSingleUpdate(this,
            new CurrentLocation.LocationCallback() {

                @Override public void onNewLocationAvailable(GPSCoordinates location) {
                    Log.d("Location", "my location is " + location.latitude + " : " + location.longitude);
                    startPoint = new GeoPoint(location.latitude, location.longitude);

                    // My Location Overlay
                    Marker marker=new Marker(map);
                    marker.setPosition(startPoint);
                    marker.setTitle("Your Location");
                    marker.showInfoWindow();
                    map.getOverlays().add(marker);
                    map.invalidate();
                    //This geopoint is currently static and shows a single location everytime the app is executed
                    mapController.setCenter(startPoint);

                    takeScreenshot();
                }
            });}

private void takeScreenshot() {

    HandlerThread handlerThread = new HandlerThread("content_observer");
    handlerThread.start();
    final Handler handler = new Handler(handlerThread.getLooper())
    {

        @Override
        public void handleMessage(Message msg)
        {
            super.handleMessage(msg);
        }
    };

    getContentResolver().registerContentObserver(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
            true,
            new ContentObserver(handler)
            {
                @Override
                public boolean deliverSelfNotifications()
                {

                    return super.deliverSelfNotifications();
                }

                @Override
                public void onChange(boolean selfChange)
                {
                    super.onChange(selfChange);
                }

                @Override
                public void onChange(boolean selfChange, Uri uri)
                {


                    String screen_shot = "1";

                    if (uri.toString().matches(MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString() + "/[0-9]+"))
                    {

                        Cursor cursor = null;
                        try
                        {
                            cursor = getContentResolver().query(uri, new String[]
                                    {
                                            MediaStore.Images.Media.DISPLAY_NAME,
                                            MediaStore.Images.Media.DATA
                                    }, null, null, null);

                            if (cursor != null && cursor.moveToFirst())
                            {
                                final String fileName = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME));
                                final String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));



                            }
                        }
                        finally
                        {
                            if (cursor != null)
                            {
                                cursor.close();
                            }
                        }
                    }
                    super.onChange(selfChange, uri);
                }
            }
    );
}

}

当地图使用用户当前位置完成加载时,我想采用该屏幕的自动屏幕截图以供进一步使用。我添加了一个代码来截取屏幕截图,但我无法实现相同的目标。我应该做些什么改变?

2 个答案:

答案 0 :(得分:1)

您是否在代码中添加了以下权限。如果您使用

,则为API级别23及更高版本添加Dangerous / Runtime权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

答案 1 :(得分:0)

只需简单地使用代码进行截屏

 HandlerThread handlerThread = new HandlerThread("content_observer");
    handlerThread.start();
    final Handler handler = new Handler(handlerThread.getLooper())
    {

        @Override
        public void handleMessage(Message msg)
        {
            super.handleMessage(msg);
        }
    };

    getContentResolver().registerContentObserver(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
            true,
            new ContentObserver(handler)
            {
                @Override
                public boolean deliverSelfNotifications()
                {

                    return super.deliverSelfNotifications();
                }

                @Override
                public void onChange(boolean selfChange)
                {
                    super.onChange(selfChange);
                }

                @Override
                public void onChange(boolean selfChange, Uri uri)
                {


                    screen_shot = "1";

                    if (uri.toString().matches(MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString() + "/[0-9]+"))
                    {

                        Cursor cursor = null;
                        try
                        {
                            cursor = getContentResolver().query(uri, new String[]
                                    {
                                    MediaStore.Images.Media.DISPLAY_NAME,
                                    MediaStore.Images.Media.DATA
                            }, null, null, null);

                            if (cursor != null && cursor.moveToFirst())
                            {
                                final String fileName = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME));
                                final String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));



                            }
                        }
                        finally
                        {
                            if (cursor != null)
                            {
                                cursor.close();
                            }
                        }
                    }
                    super.onChange(selfChange, uri);
                }
            }
    );