1

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

  • can you put your ondraw() method – king Feb 26 '16 at 22:10
  • Can you explain how you are expecting your "canvas code" to work? I'm having a hard time figuring out what you're intending to do there. Also, your AsyncTask seems completely unrelated to your canvas code. – Doug Stevenson Feb 26 '16 at 22:22
  • Well, I am just drawing points from table. I have set `bitmap` as Bitmap that Canvas is drawing onto. So I assumed that when I save `bitmap` as picture, those points will be visible. There's no problem with code, I only don't understand why, when I save that bitmap as a picture, the picture is blank. –  Feb 26 '16 at 22:37

1 Answers1

0

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.

king
  • 507
  • 1
  • 4
  • 17
  • That part is going just fine. I can't use `drawPoints(...)` becouse I need to change `paint` every iteration. And problem is not with drawing, but with the picture I get when I save the `bitmap` onto which is Canvas drawing. The picture is just blank, without those points I draw there. –  Feb 26 '16 at 22:50
  • what I want to say is that your drawing on the wrong canvas. Why you say you can't use drawPoint() ? you use it in your code. You are drawing on the canvas that onDraw give you, so it is detach from your bitmap – king Feb 26 '16 at 22:54
  • So when I want the displayed bitmap I need to get that view's Bitmap? –  Feb 26 '16 at 23:03
  • Yep, when the guy click on the button, you return your bitmap (view.getBitmap()). – king Feb 26 '16 at 23:16
  • Alright, the problem was exactly what you suggested. I used this as reference to correct it http://stackoverflow.com/a/9595919/5465676 . Thank you! I will accept your answer when you edit it, now it's not clear what the solution is. –  Feb 26 '16 at 23:39