user5465676
user5465676

Reputation:

Saved bitmap is just blank file

In my application, I'am drawing on Canvas. When user clicks on save icon, I retrieve a bitmap canvas is drawing on and save it as PNG file. Everything works fine except that the picture is blank. When saved as JPEG, it's just full black.

Canvas code

private Bitmap bitmap;

@Override
protected void onSizeChanged(int w, int h, int oldW, int oldH) {
    super.onSizeChanged(w, h, oldW, oldH);

    bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    canvas = new Canvas(bitmap);
}

public Bitmap getBitmap() {
    return bitmap;
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    float x;
    float y;

    for(int i = table.length - 1; i >= 0; i--) {
        x = table[i][0];       
        y = table[i][1];
        alpha = (int) table[i][2];

        paint.setAlpha(alpha);
        canvas.drawPoint(x, y, paint);
    }
}

Activity just calls AsyncTask so I won't post it here. Here is AsyncTask

@Override
protected Boolean doInBackground(Bitmap... params) {
    Bitmap bitmap = params[0];
    FileOutputStream outStream = null;

    String filename = name + ".png";
    String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString();

    File file = new File(dir, filename);

    try {
        outStream = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } finally {
        try {
            if (outStream != null) {
                outStream.flush();
                outStream.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return true;
}

And this is what should be in the picture enter image description here

EDIT

Added onDraw() method

Upvotes: 1

Views: 1321

Answers (1)

king
king

Reputation: 507

You have to do create your own Canvas in your custom view and draw in it:

mCanvas = new Canvas(bitmap);

and in your ondraw()

mCanvas.drawPoint(parm1, parm2, param3);

The problem is that you don't draw on the right canvas.

Upvotes: 0

Related Questions