title: Save a canvas for a future use
body: <p>I have a view that extends from <code>EditText</code> that allows the user to write a text and then draw with the finger in the same view.</p>
  
  <p>What I want is to save that item for a future use, I'm thinking on when the app is destroyed and then opened the draws are removed. What I want is a way to save this drawing for the next time that the user opens the app.</p>
  
  <p>For the text I used <code>sharedPreferences</code> that save the text in the <code>onPause</code>, and for the draw I don't know how can be saved.</p>
  
  <p>I think that saving the draw as an image file not was a way to save it, but I don't know.</p>
  
  <p>EDIT:</p>
  
  <p>I tried saving the Bitmap as a jpeg and then drawing to the canvas. But it don't work, at the end, it shows the object in color black and don't allow me to draw.</p>
  
  <p>My code is the following, at the end are written the two methods that I created, but that they don't work well. They are <code>saveBitmap()</code> and <code>openBitmap()</code>:</p>
  
  <pre><code>import java.io.File;
  import java.io.FileNotFoundException;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.io.OutputStream;
  
  import android.content.Context;
  import android.graphics.*;
  import android.os.Environment;
  import android.text.Editable;
  import android.text.TextWatcher;
  import android.util.AttributeSet;
  import android.view.MotionEvent;
  import android.widget.EditText;
  
  public class DrawEditText extends EditText {
  
      private Bitmap  mBitmap;
      private Canvas  mCanvas;
      private Path    mPath;
      private Paint   mBitmapPaint;
      private Paint   mPaint;
      private MaskFilter  mEmboss;
      private MaskFilter  mBlur;
      private int brush;        
      private Boolean drawing;
      private int maxLines;
  
      public DrawEditText(Context c) {
          super(c);
  
  
          //This initializes all the objects
          mPath = new Path();
          mBitmapPaint = new Paint(Paint.DITHER_FLAG);
          mBitmapPaint.setColor(Color.TRANSPARENT);
  
          mPaint = new Paint();
          mPaint.setColor(Color.BLACK);
          mPaint.setStyle(Paint.Style.STROKE);
          mPaint.setStrokeWidth(5);
          mPaint.setAntiAlias(true);
  
          mEmboss = new EmbossMaskFilter(new float[] { 1, 1, 1 },
                  0.4f, 6, 3.5f);
  
          mBlur = new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL);
  
          drawing = false;
          brush = -1;
          maxLines = 20;
      }
      public DrawEditText(Context c, AttributeSet attrs) {
          super(c, attrs);
  
          //This initializes all the objects
          mPath = new Path();
          mBitmapPaint = new Paint(Paint.DITHER_FLAG);
  
          mPaint = new Paint();
          mPaint.setColor(Color.BLACK);
          mPaint.setStyle(Paint.Style.STROKE);
          mPaint.setStrokeWidth(5);
          mPaint.setAntiAlias(true);
  
          mEmboss = new EmbossMaskFilter(new float[] { 1, 1, 1 },
                  0.4f, 6, 3.5f);
  
          mBlur = new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL);
  
          drawing = false;
          brush = -1;
          maxLines = 20;
      }
  
      @Override
      protected void onSizeChanged(int w, int h, int oldw, int oldh) {
          super.onSizeChanged(w, h, oldw, oldh);
          mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
          mCanvas = new Canvas(mBitmap);
      }
  
      @Override
      protected void onDraw(Canvas canvas) {
          canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
          canvas.drawPath(mPath, mPaint);
          super.onDraw(canvas);
      }
  
      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);
          // commit the path to our offscreen
          mCanvas.drawPath(mPath, mPaint);
          // kill this so we don't double draw
          mPath.reset();
      }
  
      @Override
      public boolean onTouchEvent(MotionEvent event) {
          float x = event.getX();
          float y = event.getY();
  
          //This conditional makes that if not it's drawing no points are saved and no points are drawed
          if (drawing){
              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();
                      invalidate();
                      break;
              }
              return true;
          } else {
              return super.onTouchEvent(event);
          }
      }
  
      public void changePaint(int stroke, int color){
          mPaint.setColor(color);
          mPaint.setStrokeWidth(stroke);
      }
      public void clear(){
          mCanvas.drawColor(0x00AAAAAA);
          mCanvas.drawColor( 0, PorterDuff.Mode.CLEAR );
          setText("");
      }
      public void changeBrush(int id){
          //String[] ids={"emboss", "blur", "another"};
          switch (id) {
          case 0:
              mPaint.setMaskFilter(mEmboss);
              brush = 0;
              break;
          case 1:
              mPaint.setMaskFilter(mBlur);
              brush = 1;
              break;
          case 2:
              mPaint.setMaskFilter(null); 
              brush = -1;
              break;
          default:
              mPaint.setMaskFilter(null);
              brush = -1;
              break;
          }
      }
      public int getBrush(){
          //String[] ids={"emboss", "blur", "another"};
          return brush;
      }
      public void eraser(){
          mPaint.setMaskFilter(null); 
          mPaint.setColor(0x00AAAAAA);
      }
  
      public void setDrawing(Boolean drawing){
          this.drawing = drawing;
      }
  
      public Boolean isDrawing(){
          return drawing;
      }
  
      @Override
      protected void onFinishInflate() {
          super.onFinishInflate();
  
          TextWatcher watcher = new TextWatcher() {
  
              private String text;
              private int beforeCursorPosition = 0;
  
              @Override
              public void onTextChanged(CharSequence s, int start, int before,
                      int count) {
              }
  
              @Override
              public void beforeTextChanged(CharSequence s, int start, int count,
                      int after) {
                  text = s.toString();
                  beforeCursorPosition = start;
              }
  
              @Override
              public void afterTextChanged(Editable s) {
  
                  /* turning off listener */
                  removeTextChangedListener(this);
  
                  /* handling lines limit exceed */
                  if (DrawEditText.this.getLineCount() &gt; maxLines) {
                      DrawEditText.this.setText(text);
                      DrawEditText.this.setSelection(beforeCursorPosition);
                  }
  
                  /* turning on listener */
                  addTextChangedListener(this);
  
              }
          };
  
          this.addTextChangedListener(watcher);
      }
  
      @Override
      public int getMaxLines() {
          return maxLines;
      }
  
      @Override
      public void setMaxLines(int maxLines) {
          this.maxLines = maxLines;
      }
  
      public void saveBitmap(){
          mCanvas.setBitmap(mBitmap);
          File sd = Environment.getExternalStorageDirectory();
          File myDir = new File(sd + "/tweeet");    
          myDir.mkdirs();
          File f = new File(myDir, ".drawing.jpeg");
          try {
              if (myDir.canWrite()) {
                   f.createNewFile();
                   OutputStream os = new FileOutputStream(f);
                   mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, os);
                   os.close();
              }
         } catch (FileNotFoundException e) {
              e.printStackTrace();
         } catch (IOException e) {
              e.printStackTrace();
         }
      }
      public void openBitmap(){
          File sd = Environment.getExternalStorageDirectory();
          File myDir = new File(sd + "/tweeet");    
          myDir.mkdirs();
          File f = new File(myDir, ".drawing.jpeg");
          mBitmap = BitmapFactory.decodeFile(f.getPath());
      }
  }
  </code></pre>