捕获屏幕快照GoogleMap的Andr​​oid的API V2的 [英] Capture screen shot of GoogleMap Android API V2

查看:146
本文介绍了捕获屏幕快照GoogleMap的Andr​​oid的API V2的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最后更新

该功能请求已经履行了谷歌。请参阅低于这个答案。

The feature request has been fulfilled by Google. Please see this answer below.

原始的问题

使用旧版本的谷歌地图Android的API中,我能捕捉到谷歌地图的截图通过社交媒体分享。我用下面的code捕捉屏幕截图,图像保存到一个文件和它的工作很大的:

Using the old version of the Google Maps Android API, I was able to capture a screenshot of the google map to share via social media. I used the following code to capture the screenshot and save the image to a file and it worked great:

public String captureScreen()
{
    String storageState = Environment.getExternalStorageState();
    Log.d("StorageState", "Storage state is: " + storageState);

    // image naming and path  to include sd card  appending name you choose for file
    String mPath = this.getFilesDir().getAbsolutePath();

    // create bitmap screen capture
    Bitmap bitmap;
    View v1 = this.mapView.getRootView();
    v1.setDrawingCacheEnabled(true);
    bitmap = Bitmap.createBitmap(v1.getDrawingCache());
    v1.setDrawingCacheEnabled(false);

    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 = "";
    }

    return filePath;
}

不过,所用的API V2新的GoogleMap对象没有一个getRootView()方法类似图形页面一样。

However, the new GoogleMap object used by V2 of the api does not have a "getRootView()" method like MapView does.

我试图做到这一点:

    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.basicMap);

    View v1 = mapFragment.getView();

不过,我得到没有任何地图内容和截图如下:

But the screenshot that I get does not have any map content and looks like this:

有没有人想出如何利用新的谷歌地图的Andr​​oid API V2的截图?

Has anyone figured out how to take a screenshot of the new Google Maps Android API V2?

更新

我也试图让rootView这种方式:

I also tried to get the rootView this way:

View v1 = getWindow().getDecorView().getRootView();

这会导致一个截图,其中包括屏幕顶部的操作栏,但地图上仍是空白像我附上了截图。

This results in a screenshot that includes the action bar at the top of the screen, but the map is still blank like the screenshot I attached.

更新

一个功能请求已经提交给谷歌。请到明星的功能要求,如果这是你希望谷歌在将来添加的东西:的添加截屏的能力,谷歌地图API V2

A feature request has been submitted to Google. Please go star the feature request if this is something you want google to add in the future: Add screenshot ability to Google Maps API V2

推荐答案

更​​新 - 谷歌已经增加了一个快照方法**!:

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

有关的方法采取了截屏Android的谷歌地图API V2的OpenGL层的功能要求已经得到满足。

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)

和调用:

公众最终无效快照(GoogleMap.SnapshotReadyCallback回调)

为例,需要一个截屏,然后presents标准的图像共享选项:

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

文档是<一个href="https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/GoogleMap.SnapshotReadyCallback">here

这篇关于捕获屏幕快照GoogleMap的Andr​​oid的API V2的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆