title: How to set google maps as a background on bitmap image
body: <p>I am working on project which needs to add drawing feature on google map.
  my problem is after drawing on google map when i tried to save the image in external memory. It saved in memory but can't get background of google map.</p>
  
  <p>This is image which created after save button, i got current location button,edit button, google text but not map:</p>
  
  <p><img src="https://i.stack.imgur.com/kAM2a.jpg" alt="this is image which created after save button, i got current location button,edit button, google text but not map"> </p>
  
  <p>here is my xml file.</p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      android:orientation="vertical"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:id="@+id/map_layout"&gt;
  &lt;LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="horizontal"&gt;
  
      &lt;EditText
          android:id="@+id/map_search"
          android:layout_width="150dp"
          android:layout_height="wrap_content"
          android:layout_marginLeft="8dp"
          android:layout_marginRight="8dp"
          android:hint="search....."
          android:textColor="@android:color/black"
          android:textStyle="bold"
          android:visibility="visible"
          app:layout_constraintLeft_toLeftOf="parent"
          app:layout_constraintRight_toRightOf="parent"
          /&gt;
      &lt;Button
          android:id="@+id/button2"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_gravity="right"
          android:onClick="geoLocate"
          android:text="GO" /&gt;
  
      &lt;ImageButton
          android:id="@+id/exit_edit_mode"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:src="@drawable/exit_button"
          android:background="@null"
          android:layout_marginTop="8dp"
          android:layout_marginRight="10dp"
          android:layout_marginLeft="10dp"
         /&gt;
  
  
      &lt;ImageButton
          android:id="@+id/save_button"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:src="@drawable/save_32"
          android:background="@null"
          android:layout_marginTop="8dp"
          android:layout_marginRight="10dp"
  
          /&gt;
  
  &lt;/LinearLayout&gt;
  &lt;FrameLayout
      android:id="@+id/signature"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"&gt;   
      &lt;fragment
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:name="com.google.android.gms.maps.MapFragment"
          android:id="@+id/mapFragment"/&gt;
      &lt;ImageButton
          android:id="@+id/edit_map"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:src="@drawable/edit_button"
          android:background="@null"
          android:layout_marginLeft="320dp"
          android:layout_marginTop="430dp"/&gt;
  
  &lt;/FrameLayout&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>this is my map activity code from where i am calling sava method.</p>
  
  <pre><code>private void drawOnMap() {
          mcontent = (FrameLayout) findViewById(signature);
          save_button = (ImageButton) findViewById(R.id.save_button);
          exit_edit_mode = (ImageButton) findViewById(R.id.exit_edit_mode);
          mSignature = new signature(this, null);
          mcontent.addView(mSignature);
  
          save_button.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  mSignature.save();
              }
          });
          exit_edit_mode.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  mSignature.clear();
                  mcontent.removeView(mSignature);
                  mGoogleMap.getUiSettings().setScrollGesturesEnabled(true);
              }
          });
      }
  </code></pre>
  
  <p>here i define save method which is called from the map activity</p>
  
  <pre><code> public void save() {
          Bitmap returnedBitmap = Bitmap.createBitmap(mcontent.getWidth(),
                  mcontent.getHeight(),Bitmap.Config.ARGB_8888);
          Canvas canvas = new Canvas(returnedBitmap);
          Drawable bgDrawable = mcontent.getBackground();
          if (bgDrawable != null)
              bgDrawable.draw(canvas);
          else
              canvas.drawColor(Color.MAGENTA);
          mcontent.draw(canvas);
  
          ByteArrayOutputStream baso=null;
          File file=null;
          try{
              String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
                      Locale.getDefault()).format(new Date());
              // compress and write output stream
  
              baso=new ByteArrayOutputStream();
              returnedBitmap.compress(Bitmap.CompressFormat.JPEG,40,baso);
  
              file=new File(Environment.getExternalStorageDirectory().getAbsoluteFile()+file.separator+"pictures/"+timeStamp+".JPEG");
              file.createNewFile();
  
              FileOutputStream fos=new FileOutputStream(file);
              fos.write(baso.toByteArray());
              fos.close();
              Toast.makeText(signature.this.getContext(), "Map Saved to memory", Toast.LENGTH_LONG).show();
          }catch(Exception e){
              e.printStackTrace();
          }
      }
  </code></pre>
  
  <p>this is how i drawing on google map:</p>
  
  <p><img src="https://i.stack.imgur.com/cN67O.png" alt="this is how i drawing on google map"></p>
  
  <p>this is my signature class which help me to do free hand drawing on google map.</p>
  
  <pre><code>public class signature extends View {
      static final float STROKE_WIDTH = 10f;
      static final float HALF_STROKE_WIDTH = STROKE_WIDTH / 2;
  
      Paint paint = new Paint();
      Path path = new Path();
  
      float lastTouchX;
      float lastTouchY;
      final RectF dirtyRect = new RectF();
  
      public signature(Context context, AttributeSet attrs) {
          super(context, attrs);
          paint.setAntiAlias(true);
          paint.setColor(Color.GREEN);
          paint.setStyle(Paint.Style.STROKE);
          paint.setStrokeJoin(Paint.Join.ROUND);
          paint.setStrokeWidth(STROKE_WIDTH);
      }
  
      public void clear() {
          path.reset();
          invalidate();
      }
  
      public void save() {
          Bitmap returnedBitmap = Bitmap.createBitmap(mcontent.getWidth(),
                  mcontent.getHeight(),Bitmap.Config.ARGB_8888);
          Canvas canvas = new Canvas(returnedBitmap);
          Drawable bgDrawable = mcontent.getBackground();
          System.out.println("#######################"+bgDrawable);
          if (bgDrawable != null)
              bgDrawable.draw(canvas);
          else
              canvas.drawColor(Color.WHITE);
          mcontent.draw(canvas);
  
          ByteArrayOutputStream baso=null;
          File file=null;
          try{
              String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
                      Locale.getDefault()).format(new Date());
              // compress and write output stream
  
              System.out.println("@@@@@@@@@@@@@@@@"+timeStamp);
              baso=new ByteArrayOutputStream();
              returnedBitmap.setHasAlpha(true);
              returnedBitmap.compress(Bitmap.CompressFormat.JPEG,40,baso);
  
              file=new File(Environment.getExternalStorageDirectory().getAbsoluteFile()+file.separator+"pictures/"+timeStamp+".JPEG");
              file.createNewFile();
  
              FileOutputStream fos=new FileOutputStream(file);
              fos.write(baso.toByteArray());
              fos.close();
              Toast.makeText(signature.this.getContext(), "Map Saved to memory", Toast.LENGTH_LONG).show();
              /*Intent intent = new Intent();
              intent.putExtra("byteArray", baso.toByteArray());
              setResult(1, intent);
              finish();*/
          }catch(Exception e){
              e.printStackTrace();
          }
      }
  
      @Override
      protected void onDraw(Canvas canvas) {
          // TODO Auto-generated method stub
          canvas.drawPath(path, paint);
      }
  
      @Override
      public boolean onTouchEvent(MotionEvent event) {
          float eventX = event.getX();
          float eventY = event.getY();
          //save.setEnabled(true);
  
          switch (event.getAction()) {
              case MotionEvent.ACTION_DOWN:
                  path.moveTo(eventX, eventY);
                  lastTouchX = eventX;
                  lastTouchY = eventY;
                  return true;
  
              case MotionEvent.ACTION_MOVE:
              case MotionEvent.ACTION_UP:
                  resetDirtyRect(eventX, eventY);
                  int historySize = event.getHistorySize();
                  for (int i = 0; i &lt; historySize; i++) {
                      float historicalX = event.getHistoricalX(i);
                      float historicalY = event.getHistoricalY(i);
                      path.lineTo(historicalX, historicalY);
                  }
                  path.lineTo(eventX, eventY);
                  break;
          }
  
          invalidate((int) (dirtyRect.left - HALF_STROKE_WIDTH),
                  (int) (dirtyRect.top - HALF_STROKE_WIDTH),
                  (int) (dirtyRect.right + HALF_STROKE_WIDTH),
                  (int) (dirtyRect.bottom + HALF_STROKE_WIDTH));
  
          lastTouchX = eventX;
          lastTouchY = eventY;
  
          return true;
      }
  
      private void resetDirtyRect(float eventX, float eventY) {
          dirtyRect.left = Math.min(lastTouchX, eventX);
          dirtyRect.right = Math.max(lastTouchX, eventX);
          dirtyRect.top = Math.min(lastTouchY, eventY);
          dirtyRect.bottom = Math.max(lastTouchY, eventY);
      }
  }
  </code></pre>
  
  <p>i will be very thankful if some one get me rid of it.
  thank you.</p>
  
  <p><strong>update</strong></p>
  
  <p><strong>when i try this method it save only map not drawing part which i made on google map. when i use canvas below the bitmap.compress it save only map but when i use canvas above bitmap.compress it again appear black screen as shown in above pic.</strong></p>
  
  <pre><code> public void CaptureMapScreen() {
          GoogleMap.SnapshotReadyCallback callback = new GoogleMap.SnapshotReadyCallback() {
              Bitmap bitmap;
  
              @Override
              public void onSnapshotReady(Bitmap snapshot) {
                  // TODO Auto-generated method stub
                  bitmap = snapshot;
                  try {
                      FileOutputStream out = new FileOutputStream("/mnt/sdcard/"
                              + "MyMapScreen" + System.currentTimeMillis()
                              + ".png");
  
                      // above "/mnt ..... png" =&gt; is a storage path (where image will be stored) + name of image you can customize as per your Requirement
  
                      Canvas convas = new Canvas(bitmap);
  
                      mcontent.draw(convas);
                     // mcontent.getBackground();
                      bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
  
                      Toast.makeText(getContext(),"map saved",Toast.LENGTH_SHORT).show();
                  } catch (Exception e) {
                      e.printStackTrace();
                  }
              }
          };
  
          mGoogleMap.snapshot(callback);
  
      }
  </code></pre>