KeirDavis
KeirDavis

Reputation: 665

Saving a canvas to Bitmap then saving the Bitmap

This is surrounded in a try, catch for the writing part, but if it isn't in a try catch methods, the app seems to crash in the emulator. I'm trying to save the canvas as a bitmap, then saving the bitmap to the storage...

screenshot = Bitmap.createBitmap(screenshot, 0, 0, 0, 0);
Canvas can = new Canvas(screenshot);
int i = 0;
String filename = "EnderShot";
while (new File(filename + i + ".png") != null){
    FileOutputStream fos = null;
    fos = openFileOutput(filename + i + ".png", Context.MODE_PRIVATE);
    fos.write(screenshot.getByteCount());
    fos.close();
}

This also saves it... So if anyone could work it out?

Upvotes: 1

Views: 252

Answers (1)

IAmGroot
IAmGroot

Reputation: 13865

Anything you draw on the canvas, will infact be drawn on the underlying Bitmap.

in this case: screenshot

So you already have the bitmap of the canvas, and don't need to convert the canvas to bitmap.

TO save the bitmap to file do

try {
   FileOutputStream out = new FileOutputStream(filename + i + ".png");
   screenshot.compress(Bitmap.CompressFormat.PNG, 90, out);
   out.flush();
   out.close();
} catch (Exception e) {
   e.printStackTrace();
}

Upvotes: 1

Related Questions