title: draw on canvas without removing the background image on erase function
body: <p>I am trying to draw and erase on image and then save it to sd card. I got success in drawing and saving it to sd card, but when i checked the save image in sd card I got a black background image not the image at i draw.</p>
  
  <p>Please suggest me how to apply image as a canvas background and erase draw text without losing the background image. I have seen various example on stack overflow but my bad no one is working for me, pls help.</p>
  
  <p>What I need :  </p>
  
  <p><img src="https://i.stack.imgur.com/bWPdz.png" alt="enter image description here"></p>
  
  <p>What I am getting from sd card :</p>
  
  <p><img src="https://i.stack.imgur.com/fvm7Q.jpg" alt="enter image description here"></p>
  
  <p>Here is my effort :</p>
  
  <pre><code>public class HomeActivity extends Activity {
  private Bitmap DrawBitmap;
  private Canvas mCanvas;
  private Path mPath;
  private Paint DrawBitmapPaint;
  RelativeLayout Rl;
  CustomView View;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.home);
  
      ActionBar actionBar = getActionBar();
      actionBar.setDisplayShowTitleEnabled(false);
  
      this.loadActivity();
  }
  
  private Paint mPaint;
  
  public class CustomView extends View {
  
      public CustomView(Context c) {
  
          super(c);
  
          create_image();
  
          setLayerType(android.view.View.LAYER_TYPE_SOFTWARE, mPaint);
  
      }
  
      @Override
      protected void onSizeChanged(int w, int h, int oldw, int oldh) {
          // mCanvas.drawColor(Color.BLUE);
  
          super.onSizeChanged(w, h, oldw, oldh);
      }
  
      @Override
      protected void onDraw(Canvas canvas) {
          setDrawingCacheEnabled(true);
          canvas.drawBitmap(DrawBitmap, 0, 0, DrawBitmapPaint);
          canvas.drawPath(mPath, mPaint);
  
          canvas.drawRect(mY, 0, mY, 0, DrawBitmapPaint);
      }
  
      private float mX, mY;
      private static final float TOUCH_TOLERANCE = 4;
  
      private void touch_start(float x, float y) {
          mPath.reset();
          mPath.moveTo(x, y);
          mX = x;
          mY = y;
      }
  
      private void touch_move(float x, float y) {
          float dx = Math.abs(x - mX);
          float dy = Math.abs(y - mY);
          if (dx &gt;= TOUCH_TOLERANCE || dy &gt;= TOUCH_TOLERANCE) {
              mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
              mX = x;
              mY = y;
          }
      }
  
      private void touch_up() {
          mPath.lineTo(mX, mY);
  
          mCanvas.drawPath(mPath, mPaint);
  
          mPath.reset();
      }
  
      @Override
      public boolean onTouchEvent(MotionEvent event) {
          float x = event.getX();
          float y = event.getY();
  
          switch (event.getAction()) {
          case MotionEvent.ACTION_DOWN:
              touch_start(x, y);
              invalidate();
              break;
          case MotionEvent.ACTION_MOVE:
              touch_move(x, y);
              invalidate();
              break;
          case MotionEvent.ACTION_UP:
              touch_up();
              performClick();
              invalidate();
              break;
          }
          return true;
      }
  
      public void clear() {
          create_image();
  
          // Added later..
          mPaint = new Paint();
          mPaint.setAntiAlias(true);
          mPaint.setDither(true);
          mPaint.setColor(Color.BLACK);
          mPaint.setStyle(Paint.Style.STROKE);
          // mCanvas.drawColor(Color.BLUE);
          mPaint.setStrokeJoin(Paint.Join.ROUND);
          mPaint.setStrokeCap(Paint.Cap.ROUND);
          mPaint.setStrokeWidth(12);
          this.invalidate();
      }
  
  }
  
  public void loadActivity() {
  
      View = new CustomView(this);
      Rl = (RelativeLayout) findViewById(R.id.Rel);
      Rl.addView(View);
      mPaint = new Paint();
      mPaint.setAntiAlias(true);
      mPaint.setDither(true);
      // mCanvas.drawColor(Color.BLUE);
      mPaint.setStyle(Paint.Style.STROKE);
      mPaint.setStrokeJoin(Paint.Join.ROUND);
      mPaint.setStrokeCap(Paint.Cap.ROUND);
      mPaint.setStrokeWidth(10);
  
  }
  
  public void create_image() {
  
      DisplayMetrics displaymetrics = new DisplayMetrics();
      getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
      int screenWidth = displaymetrics.widthPixels;
      int screenHeight = displaymetrics.heightPixels;
      DrawBitmap = Bitmap.createBitmap(screenWidth, screenHeight,
              Bitmap.Config.ARGB_4444);
  
      mCanvas = new Canvas(DrawBitmap);
      mCanvas.drawColor(R.drawable.myBackgroundImage);
  
      mPath = new Path();
      DrawBitmapPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
  
  }
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      getMenuInflater().inflate(R.menu.action_menu, menu);
  
      return true;
  }
  
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
      mPaint.setXfermode(null);
      switch (item.getItemId()) {
      case R.id.erase:
          mPaint.setStrokeWidth(40);
  
          mPaint.setColor(Color.BLUE);
          mPaint.setStrokeWidth(10);
  
          break;
      case R.id.DELETE:
  
          View.clear();
          mCanvas.drawColor(Color.BLUE);
          break;
      case R.id.color_Red:
          mPaint.setStrokeWidth(8);
          mPaint.setColor(Color.RED);
  
          break;
      case R.id.color_Green:
          mPaint.setStrokeWidth(8);
          mPaint.setColor(Color.GREEN);
  
          break;
      case R.id.color_Yello:
          mPaint.setStrokeWidth(8);
          mPaint.setColor(Color.YELLOW);
  
          break;
  
      case R.id.color_Black:
          mPaint.setStrokeWidth(8);
          mPaint.setColor(Color.BLACK);
  
          break;
  
      case R.id.draw:
          mPaint.setStrokeWidth(8);
          mPaint.setXfermode(null);
  
          break;
  
      case R.id.Save:
          String root = Environment.getExternalStorageDirectory().toString();
          File myDir = new File(root + "/saved_images");
          myDir.mkdirs();
          Random generator = new Random();
          int n = 10000;
          n = generator.nextInt(n);
          String fname = "Image-" + n + ".jpg";
          File file = new File(myDir, fname);
          if (file.exists())
              file.delete();
  
          try {
              FileOutputStream out = new FileOutputStream(file);
              DrawBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
              out.flush();
              out.close();
              Toast.makeText(this, "File Saved ::" + fname,
                      Toast.LENGTH_SHORT).show();
  
              sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
                      Uri.parse("file://"
                              + Environment.getExternalStorageDirectory())));
  
          } catch (Exception e) {
              Toast.makeText(this, "ERROR" + e.toString(), Toast.LENGTH_SHORT)
                      .show();
          }
      }
  
      return super.onOptionsItemSelected(item);
  }
  </code></pre>