Android Google Map V2截图

时间:2013-07-19 12:35:48

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

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

我做了很多R& D却发现谷歌地图V2没有提供这种设施。

请帮我解决这个问题。

2 个答案:

答案 0 :(得分: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?

答案 1 :(得分:1)

我基于这篇文章: 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();
            }
        }