Shubham Goel
Shubham Goel

Reputation: 43

How to convert canvas to bitmap in android?

I am creating an app in android i which the user draw something on canvas and save it user directory i.e. sdcard.

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.

Here is my 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);
}
}

I also want to clarify that when i use

bitmap = Bitmap.createBitmap(getDrawingCache());

method then it throws a stackoverflow exception.

Please friends help me because i am pissed off by this situation from a week.

Upvotes: 2

Views: 5356

Answers (2)

daddy in LA
daddy in LA

Reputation: 1

maybe there's logic error: when you set Move = true in move event, you forget to reset it to false, but onDraw() will be called frequently , then x1 will equal to x2, y1 to y2,you will see nothing but only a dot.

advised solution:

  1. change your logic, it's better to record the touch track in array, remember to clear it after use
  2. as the 1st answer, not to generate bitmap in onDraw() , better in another event handler such as a button click

good luck

Upvotes: 0

ben75
ben75

Reputation: 28706

Your bitmap is black because you didn't draw anything on it.

To have some drawing in the bitmap you must indeed create it with this call :

bitmap = Bitmap.createBitmap(getDrawingCache());

Then you have a stackoverflow exception simply because :

  • getDrawingCache() call onDraw(Canvas)
  • onDraw(Canvas) call getDrawnMessage(Canvas canvas)
  • getDrawnMessage(Canvas canvas) call getDrawingCache()
  • getDrawingCache() call onDraw(Canvas)
  • ...

So you real problem is the StackOverflow exception.

Solution : don't call getDrawnMessage(Canvas canvas) in the onDraw(Canvas) method.

Upvotes: 2

Related Questions