【问题标题】:Android Google Map V2 screenshot安卓谷歌地图V2截图
【发布时间】:2015-07-16 11:33:52
【问题描述】:

目前我遇到了一个问题,即如何截取谷歌地图 v2 的屏幕截图。我正在开发一种 GPS 跟踪应用程序,其中一条折线正在绘制跟踪路径。所以我想要在地图上绘制折线的地图截图。

我做了很多研发,但发现谷歌地图V2没有提供这种设施。

请帮我解决这个问题。

【问题讨论】:

  • 你能告诉我们如何在屏幕截图中管理整个跟踪路线,因为我们可以截取地图,但不知何故它不会让我们一次拍摄完整的路径。

标签: android google-maps-android-api-2


【解决方案1】:

由于您使用的是 v2,因此您的地图将被管理为 MapFragment 或 SupportMapFragment。两者都是 Fragment 的子类,可以让您访问 getView()。假设您的托管活动中有以下代码:

SupportMapFragment mapFragment = ((SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map));
View view = mapFragment.getView();

有了这个视图,你可以试试这个方法How to programmatically take a screenshot in Android?

【讨论】:

  • 我已经尝试过这段代码,但很遗憾知道它不起作用。
  • 它崩溃了吗?不能保存图片?图片不显示截图?请说明您的问题。
【解决方案2】:

我基于这篇文章: Capture screen shot of GoogleMap Android API V2

并设法毫无问题地获得了带有叠加层的地图屏幕截图:

代码如下:

public void captureScreen()
    {
        GoogleMap.SnapshotReadyCallback callback = new GoogleMap.SnapshotReadyCallback()
        {


            @Override
            public void onSnapshotReady(Bitmap snapshot) {
                try {
                    getWindow().getDecorView().findViewById(android.R.id.content).setDrawingCacheEnabled(true);
                    Bitmap backBitmap = getWindow().getDecorView().findViewById(android.R.id.content).getDrawingCache();
                    Bitmap bmOverlay = Bitmap.createBitmap(
                            backBitmap.getWidth(), backBitmap.getHeight(),
                            backBitmap.getConfig());
                    Canvas canvas = new Canvas(bmOverlay);
                    canvas.drawBitmap(snapshot, new Matrix(), null);
                    canvas.drawBitmap(backBitmap, 0, 0, null);

                    OutputStream fout = null;

                    String filePath = System.currentTimeMillis() + ".jpeg";

                    try
                    {
                        fout = openFileOutput(filePath,
                                MODE_WORLD_READABLE);

                        // Write the string to the file
                        bmOverlay.compress(Bitmap.CompressFormat.JPEG, 90, fout);
                        fout.flush();
                        fout.close();
                    }
                    catch (FileNotFoundException e)
                    {
                        // TODO Auto-generated catch block
                        Log.d("ImageCapture", "FileNotFoundException");
                        Log.d("ImageCapture", e.getMessage());
                        filePath = "";
                    }
                    catch (IOException e)
                    {
                        // TODO Auto-generated catch block
                        Log.d("ImageCapture", "IOException");
                        Log.d("ImageCapture", e.getMessage());
                        filePath = "";
                    }

                    openShareImageDialog(filePath);


                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };

       ;


        map.snapshot(callback);
    }

然后我用这个方法进行社交媒体分享:

        public void openShareImageDialog(String filePath)
        {
            File file = this.getFileStreamPath(filePath);

            if(!filePath.equals(""))
            {
                final ContentValues values = new ContentValues(2);
                values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
                values.put(MediaStore.Images.Media.DATA, file.getAbsolutePath());
                final Uri contentUriFile = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

                final Intent intent = new Intent(android.content.Intent.ACTION_SEND);
                intent.setType("image/jpeg");
                intent.putExtra(android.content.Intent.EXTRA_STREAM, contentUriFile);
                startActivity(Intent.createChooser(intent, "Share Image"));
            }
            else
            {
                //This is a custom class I use to show dialogs...simply replace this with whatever you want to show an error message, Toast, etc.
               // DialogUtilities.showOkDialogWithText(this, R.string.shareImageFailed);

                Toast.makeText(getBaseContext(),"Se pelo",Toast.LENGTH_SHORT).show();
            }
        }

【讨论】:

  • 但屏幕截图中不包含折线
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-09-15
  • 1970-01-01
  • 1970-01-01
  • 2023-03-25
  • 1970-01-01
  • 2013-04-30
  • 1970-01-01
相关资源
最近更新 更多