Google Map Android v2:位图大小为零

时间:2013-10-21 13:47:53

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

我想使用Google Map v2快照填充ImageView。

简短摘要:

我还需要为Maps XML实例充气吗?

我是否需要使用地图片段?

我是否要以编程方式创建Google地图实例并拍摄快照?

我的想法是以编程方式创建一个Maps实例,适当地调整大小(比如200x200),然后拍摄快照。但是,我的快照例程总是崩溃,“位图的高度和宽度必须> 0”

这是错误的做法吗?

XML将是这样的:

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:orientation="vertical" >
        <ImageView android:id="@+id/location_card_map"
            android:layout_width="match_parent" 
            android:layout_height="match_parent" 
            android:contentDescription="Location"
            android:clickable="true"/>
    </LinearLayout>

地图类:

    private ManagerMaps(Context context)
    {
        mContext = context;
        mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
        initializeMaps();
    }
...
            private void initializeMaps()
            {
                LinearLayout layout = new LinearLayout(mContext);
                layout.setOrientation(LinearLayout.HORIZONTAL);
                layout.setLayoutParams(new  LinearLayout.LayoutParams(200,200));//(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
                GoogleMapOptions options = new GoogleMapOptions();
                options.camera(new CameraPosition(new LatLng(0, 0), 1, 0, 0));

                // Create the MapView programmatically
                mMapView = new MapView(mContext, options);
                layout.addView(mMapView);
                    mMapView.setLayoutParams(new LayoutParams(200,200));        
                mMapView.onCreate(null);
                mMapView.onResume();
        //      setContentView(layout);        

                // Gets to GoogleMap from the MapView and does initialization stuff
                mMap = mMapView.getMap();
                if(mMap == null)
                {
                    Log.d("AppDebug", "MAP IS NULL ********");
                    return;
                }
                mMap.getUiSettings().setMyLocationButtonEnabled(true);
                mMap.setMyLocationEnabled(true);

                // Needs to call MapsInitializer before doing any CameraUpdateFactory calls
                try {
                    MapsInitializer.initialize(mContext);
                } catch (GooglePlayServicesNotAvailableException e) {
                    e.printStackTrace();
                }

            }

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

                    @Override
                    public void onSnapshotReady(Bitmap snapshot) {
                        try {
                            FileOutputStream out = new FileOutputStream(
                                    Environment.getExternalStorageDirectory()
                                            + "/MapScreenShot"
                                            + System.currentTimeMillis() + ".png");

                            snapshot.compress(Bitmap.CompressFormat.PNG, 90, out);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                };
                mMap.snapshot(callback);  // crashes in bitmap thread here with height/width zero
            }

通话方式:

                ManagerMaps maps = ManagerMaps.getInstance(getActivity());
                maps.captureMapScreen();

编辑:

崩溃发生在:mMap.snapshot(回调);

logcat的

10-22 01:36:44.300: E/AndroidRuntime(29292): FATAL EXCEPTION: Thread-3366
10-22 01:36:44.300: E/AndroidRuntime(29292): java.lang.IllegalArgumentException: width and height must be > 0
10-22 01:36:44.300: E/AndroidRuntime(29292):    at android.graphics.Bitmap.createBitmap(Bitmap.java:695)
10-22 01:36:44.300: E/AndroidRuntime(29292):    at android.graphics.Bitmap.createBitmap(Bitmap.java:674)
10-22 01:36:44.300: E/AndroidRuntime(29292):    at android.graphics.Bitmap.createBitmap(Bitmap.java:641)
10-22 01:36:44.300: E/AndroidRuntime(29292):    at maps.ag.ca.a(Unknown Source)
10-22 01:36:44.300: E/AndroidRuntime(29292):    at maps.ag.av.run(Unknown Source)
10-22 01:36:44.300: E/AndroidRuntime(29292):    at java.lang.Thread.run(Thread.java:841)

1 个答案:

答案 0 :(得分:0)

问题是由Android中可绘制资源的自动缩放和非常小的drawables的不幸组合引起的。

例如,如果您的应用仅提供drawable-xhpdi资源,则需要缩小尺寸以适应较低密度的屏幕。如果您自己不提供这些资源,则由Android自动完成。

显示密度的大小设置如下:

xhdpi:2.0 hdpi:1.5 mdpi:1.0 ldpi:0.75

有时不幸的是,当提供这些资源作为xhdpi并缩小它们时,像素大小可以被截断为0.没有适当的保护措施,Android将无法创建具有该维度的Bitmap并且崩溃显示{{ 1}}

但我不确定你的情况。所以请用LogCat更新你的问题。

更新它lyk:

IllegalArgumentException: width and height must be > 0

使用public void captureMapScreen() { SnapshotReadyCallback callback = new SnapshotReadyCallback() { @Override public void onSnapshotReady(Bitmap snapshot) { try { mView.setDrawingCacheEnabled(true); Bitmap backBitmap = mView.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); FileOutputStream out = new FileOutputStream( Environment.getExternalStorageDirectory() + "/MapScreenShot" + System.currentTimeMillis() + ".png"); bmOverlay.compress(Bitmap.CompressFormat.PNG, 90, out); } catch (Exception e) { e.printStackTrace(); } } }; mMap.snapshot(callback); } 获取地图的屏幕截图并避免使用代码

snapshot.compress(Bitmap.CompressFormat.PNG, 90, out);