【问题标题】:Take an automnatic screenshot of the screen when the map finises loading地图加载完成后自动截取屏幕截图
【发布时间】:2017-09-07 19:03:27
【问题描述】:

我正在创建一个在地图中显示用户当前位置的应用程序。我正在使用 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);
                }
            }
    );
}

}

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

【问题讨论】:

  • 出了什么问题?
  • 没有错误但我看不到屏幕截图,所以我无法理解屏幕截图是否成功
  • 您是否获得了访问存储的权限?
  • 是的,我已经获得了这个权限
  • 我的意思是 android 版本 6 及更高版本的运行时权限还添加了 e.printStackTrace();登录了解更多详情

标签: android screenshot openstreetmap


【解决方案1】:

您是否在代码中添加了以下权限。如果您正在使用,请为 API 级别 23 及更高级别添加危险/运行时权限

<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"/>

【讨论】:

  • 是的,我也添加了这些
  • @Thomas 不知道 Thomas 你犯了什么错误。它在我的代码中运行良好。我亲自测试过
  • 你能把你测试过的代码贴出来看看我犯了什么错误
  • @Thomas 提供您的邮件 ID?
  • 你拿到我的身份证了吗?
【解决方案2】:

只需简单地使用截图代码

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

【讨论】:

  • 我应该在上面给出的代码中的哪里添加这部分代码?
  • @Thomas 只需将您的“takeScreenshot”方法代码替换为上述代码即可
  • @Thomas 你能把你的新代码贴出你替换的那个吗
  • 我发布了更新的代码。我替换了你告诉的那部分代码
猜你喜欢
  • 1970-01-01
  • 2022-01-13
  • 1970-01-01
  • 2014-09-26
  • 2011-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多