title: How to merge/save image after overlaying other image?
body: <p>I am implementing an application in which there are two images . First image is static or rather say "drop-target" for android "Drag &amp; Drop" concept and second image is "draggable" . An image which is draggable is dragged and placed it over the other image . Till this point application works just fine but i'm  stuck on how to save this overlayered image.</p>
  
  <p>Here i provide what i've done till now : </p>
  
  <p><strong>DragNDropActivity.java</strong></p>
  
  <pre><code>private static final String TAG = DragNDropActivity.class.getSimpleName();
  private ImageView m_ivImage1, finalImage, sourceImage, targetImage,
          testImage;
  private int m_counter = 0;
  float m_lastTouchX, m_lastTouchY, m_posX, m_posY, m_prevX, m_prevY,
          m_imgXB, m_imgYB, m_imgXC, m_imgYC, m_dx, m_dy;
  private LinearLayout m_llTop;
  private AbsoluteLayout m_alTop;
  private Button m_btnAddView, m_btnRemove, saveImage;
  private Context m_context;
  
  @Override
  public void onCreate(Bundle savedInstanceState)
  {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.drag_drop_layout);
  
      m_context = this;
      testImage = new ImageView(m_context);
  
      m_prevX = 0;
      m_prevY = 0;
      m_imgXB = 50;
      m_imgYB = 100;
      m_imgXC = 150;
      m_imgYC = 100;
  
      m_ivImage1 = (ImageView) findViewById(R.id.ddivImage1);
      m_llTop = (LinearLayout) findViewById(R.id.ddllTop);
      m_alTop = (AbsoluteLayout) findViewById(R.id.ddalTop);
      m_btnAddView = (Button) findViewById(R.id.ddbtnAdd);
      m_btnRemove = (Button) findViewById(R.id.ddbtnRemove);
      finalImage = (ImageView) findViewById(R.id.finalImage);
      saveImage = (Button) findViewById(R.id.saveImage);
  
      m_ivImage1.setOnTouchListener(m_onTouchListener);
      m_btnAddView.setOnClickListener(m_onClickListener);
      m_btnRemove.setOnClickListener(m_onClickListener);
      saveImage.setOnClickListener(m_onClickListener);
  
      //Test code For Merging two images
      Bitmap bottomImage = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().getPath() + "/android.jpg");
      Bitmap topImage = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().getPath() + "/kitkat.jpg");
  
      Bitmap mutableBitmap = bottomImage.copy(Bitmap.Config.ARGB_8888, true);
      // use the canvas to combine them.
      // Start with the first in the constructor..
      Canvas comboImage = new Canvas(mutableBitmap);
      // Then draw the second on top of that
      comboImage.drawBitmap(topImage, 10f, 10f, null);
  
      // bottomImage is now a composite of the two.
  
      // To write the file out to the SDCard:
      OutputStream os = null;
      try
      {
          os = new FileOutputStream(Environment.getExternalStorageDirectory().getPath() + "/myNewFileName.jpg");
          mutableBitmap.compress(Bitmap.CompressFormat.PNG, 50, os);
      }
      catch (IOException e)
      {
          e.printStackTrace();
      }
      //End of Test Code
  
  }
  
  /**
   * Common click listener for clickable controls
   */
  OnClickListener m_onClickListener = new OnClickListener()
  {
  
      @Override
      public void onClick(View p_v)
      {
          switch (p_v.getId())
          {
          case R.id.ddbtnAdd:
              addView();
              break;
          case R.id.ddbtnRemove:
              removeView();
              break;
          case R.id.saveImage:
              saveImage();
              break;
          default:
              break;
          }
      }
  };
  
  @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
  private void saveImage()
  {
      Drawable sourceDrawable = sourceImage.getDrawable();
      //              Drawable targetDrawable = targetImage.getDrawable();
  
      finalImage = new ImageView(m_context);
  
      if (null != testImage.getBackground())
      {
          //imageview have image
  
          Log.d(TAG, "Image is not null in SaveImage");
          finalImage.setVisibility(View.VISIBLE);
          finalImage.setBackgroundDrawable(testImage.getBackground());
      }
      else
      {
          //imageview have no image
          Log.d(TAG, "Image is null in SaveImage");
      }
  }
  
  /**
   * Touch listener for view
   */
  OnTouchListener m_onTouchListener = new OnTouchListener()
  {
  
      @Override
      public boolean onTouch(View p_v, MotionEvent p_event)
      {
          switch (p_event.getAction())
          {
          case MotionEvent.ACTION_DOWN:
          {
              m_lastTouchX = p_event.getX();
              m_lastTouchY = p_event.getY();
  
              break;
          }
          case MotionEvent.ACTION_UP:
          {
              //It indicates that the pressure on image is released and it is stable now i.e it is not in motion.
              //Todo  Save final image
              //              saveImage();
              break;
          }
  
          case MotionEvent.ACTION_MOVE:
          {
              m_dx = p_event.getX() - m_lastTouchX;
              m_dy = p_event.getY() - m_lastTouchY;
  
              m_posX = m_prevX + m_dx;
              m_posY = m_prevY + m_dy;
  
              if (m_posX &gt; 0 &amp;&amp; m_posY &gt; 0 &amp;&amp; (m_posX + p_v.getWidth()) &lt; m_alTop.getWidth() &amp;&amp; (m_posY + p_v.getHeight()) &lt; m_alTop.getHeight())
              {
                  p_v.setLayoutParams(new AbsoluteLayout.LayoutParams(p_v.getMeasuredWidth(), p_v.getMeasuredHeight(), (int) m_posX, (int) m_posY));
  
                  m_prevX = m_posX;
                  m_prevY = m_posY;
  
              }
  
              break;
  
          }
  
          }
          return true;
      }
  };
  
  View.OnDragListener m_dragListener = new View.OnDragListener()
  {
      @Override
      public boolean onDrag(View view, DragEvent dragEvent)
      {
          Log.d(" Drag Event", "Drop Event " + dragEvent.getAction());
  
          if (dragEvent.getAction() == DragEvent.ACTION_DROP)
          {
              Log.d(" Drag Event", "Drop Ended ");
          }
  
          return true;
      }
  };
  
  /**
   * Add view dynamically for drag and drop
   */
  private void addView()
  {
      sourceImage = new ImageView(m_context);
      targetImage = new ImageView(m_context);
      TextView m_tv = new TextView(m_context);
      if (m_counter &lt; 6)
      {
          if (m_counter % 2 == 0)
          {
  
              sourceImage.setBackgroundResource(R.drawable.ic_launcher);
  
              //m_tv.setText("Hello! Drag Me! ");
              m_alTop.addView(m_tv, new LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT, ((int) m_imgXB), ((int) m_imgYB)));
              m_alTop.addView(sourceImage, new LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT, ((int) m_imgXB), ((int) m_imgYB)));
          }
          else
          {
              targetImage.setBackgroundResource(android.R.drawable.star_big_on);
              m_alTop.addView(targetImage, new LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT, ((int) m_imgXC), ((int) m_imgYC)));
          }
          m_counter++;
          if (m_counter == 6) m_btnAddView.setEnabled(false);
      }
      if (null != sourceImage.getBackground())
      {
          //imageview have image
          testImage = sourceImage;
          Log.d(TAG, "Image is not null in addView");
      }
      else
      {
          //imageview have no image
          Log.d(TAG, "Image is null");
      }
  
      //      sourceImage.setOnTouchListener(m_onTouchListener);
      targetImage.setOnTouchListener(m_onTouchListener);
      sourceImage.setOnTouchListener(m_onTouchListener);
      m_tv.setOnTouchListener(m_onTouchListener);
  }
  
  public void removeView()
  {
      m_counter = 0;
      m_alTop.removeAllViews();
      m_alTop.invalidate();
      m_btnAddView.setEnabled(true);
  }
  
  @Override
  public void onBackPressed()
  {
      this.clearView();
      super.onBackPressed();
  }
  
  @Override
  protected void onDestroy()
  {
      this.clearView();
      super.onDestroy();
  }
  
  /**
   * Clear the views and free memory
   */
  public void clearView()
  {
      if (m_context != null) m_context = null;
  
      if (m_llTop != null) m_llTop = null;
  
      if (m_alTop != null) m_alTop = null;
  
      if (m_btnAddView != null) m_btnAddView = null;
  
      if (m_btnRemove != null) m_btnRemove = null;
  
      if (m_ivImage1 != null) m_ivImage1 = null;
  }
  
  @Override
  public boolean onDrag(View view, DragEvent dragEvent)
  {
      Log.d(TAG, "Drop Event " + dragEvent.getAction());
      return true;
  }
  </code></pre>
  
  <p>Output of this code:</p>
  
  <p><img src="https://i.stack.imgur.com/y81Qd.png" alt=""></p>
  
  <p>As you can see an star over Droid. But now how to make image from this and save  as it is ?</p>