Save MapView as a bitmap

I tried both methods to save MapView as a bitmap that I found here and no one works for me. First option,

Bitmap bitMap = mMapView.getDrawingCache();             
mMapView.setDrawingCacheEnabled(true);
bitMap = mMapView.getDrawingCache(true);

and second,

Canvas offscreencanvas = new Canvas();
Bitmap bmap = Bitmap.createBitmap(mMapView.getWidth(), mMapView.getHeight(),
                    Bitmap.Config.ARGB_8888);
bmap.copy(Config.ARGB_4444, true);
offscreencanvas.setBitmap(bmap);
offscreencanvas.drawBitmap(bmap, 0, 0, null);

both results lead to a raster object with a width and height of -1, so when I try to use a raster image as a texture, it does not appear. I call the bitmap, saving the code with the click of a button, after rendering the mapview, but it still gives the same result.

Could anyone do this?

+3
source share
3 answers

I ran into similar issues trying to use the drawing cache. Some items I found helped me:

  • , . Android , . Android, -, .
  • - .

, :

// Disable caching, destroy the cache, and force a rebuild
imageView.setWillNotCacheDrawing(false);
imageView.destroyDrawingCache();
imageView.buildDrawingCache();

// Copy the drawing cache before the system recycles it
Bitmap cachedImage = Bitmap.createBitmap(imageView.getDrawingCache());

, ImageView, , MapView.

, , , . , . , . (, .) -

Bitmap mapCache = /* Get this using the previous code */
Bitmap bmap = Bitmap.createBitmap(mMapView.getWidth(), mMapView.getHeight(),
                        Bitmap.Config.ARGB_8888);
Canvas offscreencanvas = new Canvas(bmap);
offscreencanvas.drawBitmap(mapCache, 0, 0, null);
+2

MapView, , View, , :

, - , :

View v = new View(context)
{   public void onMeasure(int w, int h)
    {  setMeasuredDimension(width, height);//the desired width and height
    }}; //important
v.setDrawingCacheEnabled(true); //important
v.measure(width,height); //important the desired width and height
v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight()); //important
v.setDrawingCacheQuality(DRAWING_CACHE_QUALITY_HIGH);
v.setLayoutParams(new LayoutParams(width,height)); //important
v.buildDrawingCache(true); //important
Bitmap b = v.getDrawingCache(true);
v.buildDrawingCache(false);

, , .

+1

No need to save mapview cache drawing as bitmap. It will be cleaned up by Android very soon, so you have to put the layout on top of the mapView and get a graphic drawing image from it. This class is what I used to determine the blending point and get the map image on the image.

import java.io.IOException;
import java.util.List;
import java.util.Locale;
import android.R.layout;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

public class GoogleMap extends MapActivity {

    MapView mapView; 
    MapController mc;
    GeoPoint p;
    LinearLayout layout;
    ImageView imageview;

    /** Called when the activity is first created. */
    @SuppressWarnings("deprecation")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.testmap);
          layout   = (LinearLayout)findViewById(R.id.maplayout);
        layout.setDrawingCacheEnabled(true);
        layout.setDrawingCacheQuality(LinearLayout.DRAWING_CACHE_QUALITY_HIGH);
        imageview=(ImageView)findViewById(R.id.imageView);
        ViewTreeObserver vto = layout.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

         public void onGlobalLayout() {
          layout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
          getDrawingBitmap();
         }
        });
        mapView = (MapView) findViewById(R.id.mapview);
        mapView.setStreetView(true);
        mapView.setBuiltInZoomControls(true);
        /*LinearLayout zoomLayout = (LinearLayout)findViewById(R.id.zoom);  
        View zoomView = mapView.getZoomControls(); 

        zoomLayout.addView(zoomView, 
            new LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, 
                LayoutParams.WRAP_CONTENT));*/ 
        mapView.displayZoomControls(true);

        mc = mapView.getController();
        String coordinates[] = {"23.0504926", "72.528938925"};
        double lat = Double.parseDouble(coordinates[0]);
        double lng = Double.parseDouble(coordinates[1]);

        p = new GeoPoint(
            (int) (lat * 1E6), 
            (int) (lng * 1E6));


        mc.animateTo(p);
        mc.setZoom(17); 

        //---Add a location marker---
        MapOverlay mapOverlay = new MapOverlay();
        List<Overlay> listOfOverlays = mapView.getOverlays();
        listOfOverlays.clear();
        listOfOverlays.add(mapOverlay);        

        mapView.invalidate();
    }
    public void getDrawingBitmap(){
         Bitmap b = layout.getDrawingCache();
         imageview.setImageBitmap(b);
    }

    public void fitAgain(GeoPoint point){
        p = point;


            mc.animateTo(p,new Runnable() {

                public void run() {
                     getDrawingBitmap();

                }
            });
          //  mc.setZoom(17); 
        MapOverlay mapOverlay = new MapOverlay();
        List<Overlay> listOfOverlays = mapView.getOverlays();
        listOfOverlays.clear();
        listOfOverlays.add(mapOverlay);
        mapView.invalidate();

        System.out.println("");
    }

    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }

    class MapOverlay extends com.google.android.maps.Overlay
    {
        @Override
        public boolean draw(Canvas canvas, MapView mapView, 
        boolean shadow, long when) 
        {
            super.draw(canvas, mapView, shadow);                   

            //---translate the GeoPoint to screen pixels---
            Point screenPts = new Point();
            mapView.getProjection().toPixels(p, screenPts);

            //---add the marker---
            Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_maps_indicator_current_position);            
            canvas.drawBitmap(bmp, screenPts.x-20, screenPts.y-20, null);         
            return true;
        }

        @Override
        public boolean onTouchEvent(MotionEvent event, MapView mapView) 
        {   
            //---when user lifts his finger---
            if (event.getAction() == 1) {                
                GeoPoint p = mapView.getProjection().fromPixels(
                    (int) event.getX(),
                    (int) event.getY());

                Geocoder geoCoder = new Geocoder(
                    getBaseContext(), Locale.getDefault());
                try {
                    List<Address> addresses = geoCoder.getFromLocation(
                        p.getLatitudeE6()  / 1E6, 
                        p.getLongitudeE6() / 1E6, 1);

                    String add = "";
                    if (addresses.size() > 0) 
                    {
                        for (int i=0; i<addresses.get(0).getMaxAddressLineIndex(); 
                             i++)
                           add += addresses.get(0).getAddressLine(i) + "\n";
                    }

                    Toast.makeText(getBaseContext(), add, Toast.LENGTH_SHORT).show();
                }
                catch (IOException e) {                
                    e.printStackTrace();
                } 
                fitAgain(p);
                return true;
            }
            else                
                return false;
        }        
    }
}
0
source

All Articles