GoogleMap Android API V2 的截图 [英] Capture screen shot of GoogleMap Android API V2

查看:31
本文介绍了GoogleMap Android API V2 的截图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最终更新

Google 已满足该功能请求.请参阅

解决方案

更新 - Google 已添加快照方法**!:

已满足对 Android Google Map API V2 OpenGL 层屏幕截图方法的功能请求.

要截图,只需实现以下接口:

public abstract void onSnapshotReady(位图快照)

并调用:

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

截取屏幕截图的示例,然后显示标准的图像共享"选项:

public void captureScreen(){SnapshotReadyCallback 回调 = 新 SnapshotReadyCallback(){@覆盖public void onSnapshotReady(位图快照){//TODO 自动生成的方法存根位图 = 快照;输出流 fout = null;String filePath = System.currentTimeMillis() + ".jpeg";尝试{fout = openFileOutput(filePath,MODE_WORLD_READABLE);//将字符串写入文件bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fout);fout.flush();fout.close();}捕获 (FileNotFoundException e){//TODO 自动生成的 catch 块Log.d("ImageCapture", "FileNotFoundException");Log.d("ImageCapture", e.getMessage());文件路径 = "";}捕获(IOException e){//TODO 自动生成的 catch 块Log.d("ImageCapture", "IOException");Log.d("ImageCapture", e.getMessage());文件路径 = "";}openShareImageDialog(filePath);}};mMap.snapshot(回调);}

完成图像捕获后,它将触发标准的共享图像"对话框,以便用户可以选择他们想要的共享方式:

public void openShareImageDialog(String filePath){文件文件 = this.getFileStreamPath(filePath);if(!filePath.equals("")){最终的 ContentValues 值 = 新的 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);最终意图意图 = 新意图(android.content.Intent.ACTION_SEND);intent.setType("图片/jpeg");intent.putExtra(android.content.Intent.EXTRA_STREAM,contentUriFile);startActivity(Intent.createChooser(intent, "Share Image"));}别的{//这是我用来显示对话框的自定义类...只需将其替换为您想要显示错误消息、Toast 等的任何内容.DialogUtilities.showOkDialogWithText(this, R.string.shareImageFailed);}}

文档位于此处>

Final Update

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

Original Question

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

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

I tried to do this:

    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:

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

Update

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.

Update

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**!:

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

这篇关于GoogleMap Android API V2 的截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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