title: How to convert canvas to bitmap in android?
body: <p>I am creating an app in android i which the user draw something on canvas and save it user directory i.e. sdcard.</p>
  
  <p>But my problem is that the bitmap i save always shows a black image , i mean that the image never saves as drawn on canvas only a black image saves.</p>
  
  <p>Here is my code </p>
  
  <pre><code>import java.io.File;
  import java.io.FileNotFoundException;
  import java.io.FileOutputStream;
  
  import android.content.Context;
  import android.graphics.Bitmap;
  import android.graphics.Bitmap.CompressFormat;
  import android.graphics.Bitmap.Config;
  import android.graphics.Canvas;
  import android.graphics.Color;
  import android.graphics.Paint;
  import android.graphics.Path;
  import android.os.Environment;
  import android.view.MotionEvent;
  import android.view.View;
  import android.widget.Toast;
  
  public class DrawWrite extends View {
      float TouchXD = 0, TouchYD = 0, TouchXU = 0, TouchYU = 0, TouchXM = 0,
              TouchYM = 0; // Define touch co-ordinates
      float x1 = 0, y1 = 0, x2 = 0, y2 = 0; // Define drawing path co-ordinates
      float stroke = 2; // Define the message structure width
      int i=0;
  boolean Move = false, moveD = false, moveU = false; // Define whether the
                                                      // touch has occurred or
                                                      // not
  boolean exp = false;
  Paint paint = new Paint(); // Paint object
  Path mPath = new Path(); // Define the drawing message path
  Context context;
  
  public DrawWrite(Context context) {
      super(context);
      this.context=context;
  }
  
  @Override
  public void onDraw(Canvas canvas) {
      super.onDraw(canvas);
      invalidate();
      paint.setAntiAlias(true);
      if (DrawWriteActivity.clearScreen){
          mPath.reset();
          DrawWriteActivity.clearScreen = false;
      }
      if(DrawWriteActivity.saveImage){
          try {
              getDrawnMessage(canvas);
              DrawWriteActivity.saveImage = false;
          } catch (FileNotFoundException e) {
              e.printStackTrace();
          }
      }
      paint.setColor(Color.parseColor(DrawWriteActivity.colorProvider));
      paint.setStyle(Paint.Style.STROKE);
      paint.setStrokeWidth(DrawWriteActivity.strokeProvider);
      if (moveD == true) {
          x1 = TouchXD;
          y1 = TouchYD;
          moveD = false; 
      } else if (Move == true) {
          x2 = TouchXD;
          y2 = TouchYD;
          mPath.moveTo(x1, y1);
          mPath.lineTo(x2, y2);
          canvas.drawPath(mPath, paint);
          x1 = x2;
          y1 = y2;
      }
  }
  
  @Override
  public boolean onTouchEvent(MotionEvent event) {
      switch (event.getAction()) {
      case MotionEvent.ACTION_DOWN:
          TouchXD = (float) event.getX();
          TouchYD = (float) event.getY();
          moveD = true;
          break;
      case MotionEvent.ACTION_UP:
          TouchXU = (float) event.getX();
          TouchYU = (float) event.getY();
          moveU = true;
          break;
      case MotionEvent.ACTION_MOVE:
          TouchXD = (float) event.getX();
          TouchYD = (float) event.getY();
          Move = true;
          break;
      }
      return true;
  }
  
  public void getDrawnMessage(Canvas canvas) throws FileNotFoundException{
      Bitmap bitmap;
      setDrawingCacheEnabled(true);
      Toast.makeText(getContext(), "Toasting", Toast.LENGTH_SHORT).show();
      String root = Environment.getExternalStorageDirectory().toString();
      File imgDir = new File(root+"/ChitBak/");
      String imgName;
      bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Config.ARGB_8888);
      imgDir.mkdirs();
      imgName = "img"+i+".jpg";
      i++;
      File file = new File(imgDir,imgName);
      if(file.exists()) file.delete();
      FileOutputStream outImg = new FileOutputStream(file);
      bitmap.compress(CompressFormat.JPEG, 100, outImg);
      setDrawingCacheEnabled(false);
  }
  }
  </code></pre>
  
  <p>I also want to clarify that when i use </p>
  
  <pre><code>bitmap = Bitmap.createBitmap(getDrawingCache());
  </code></pre>
  
  <p>method then it throws a stackoverflow exception.</p>
  
  <p>Please friends help me because i am pissed off by this situation from a week.</p>