Pratik Dasa
Pratik Dasa

Reputation: 7439

Android Google Map V2 screenshot

Currrently I am stuck with one problem which is how to take screenshot of Google map v2. I am developing one application for GPS Tracking, in it one polyline is drawing tracked path. So I want screenshot for map with polyline drawn on the map as well.

I have did many R & D but found that Google map V2 is not provided such kind of facility.

Please help me to solve this issue.

Upvotes: 2

Views: 2115

Answers (2)

danielrosero
danielrosero

Reputation: 614

I based on this post: Capture screen shot of GoogleMap Android API V2

And managed to get a map screenshot with overlays without any problem:

Here is the code:

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

Then I Use this method for social media sharing:

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

Upvotes: 1

André Diermann
André Diermann

Reputation: 2783

Since you are using v2 your map will be managed be either a MapFragment or a SupportMapFragment. Both are subclasses of Fragment which should give you access to getView(). Let's assume you have the following code in your hosting activity:

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

With that view you can try this approach How to programmatically take a screenshot in Android?.

Upvotes: 1

Related Questions