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
EDIT
Added onDraw() method