Capture screen shot of GoogleMap Android API V2

Update – Google has added a snapshot method**!:

The feature request for a method to take a screen shot of the Android Google Map API V2 OpenGL layer has been fulfilled.

To take a screenshot, simply implement the following interface:

public abstract void onSnapshotReady (Bitmap snapshot)

and call:

public final void snapshot (GoogleMap.SnapshotReadyCallback callback)

Example that takes a screenshot, then presents the standard “Image Sharing” options:

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

            @Override
            public void onSnapshotReady(Bitmap snapshot) 
            {
                // TODO Auto-generated method stub
                bitmap = snapshot;

                OutputStream fout = null;

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

                try 
                {
                    fout = openFileOutput(filePath,
                            MODE_WORLD_READABLE);

                    // Write the string to the file
                    bitmap.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);
            }
        };

        mMap.snapshot(callback);
    }

Once the image is finished being captured, it will trigger the standard “Share Image” dialog so the user can pick how they’d like to share it:

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

Documentation is here

Leave a Comment