• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Saving Canvas drawing as JPG/PNG/etc..

garrydias

Newbie
Aug 9, 2010
11
0
Hello guys

I need to save a canvas result into a SD Card. Right?

In the MyView.onDraw(Canvas) i am calling:

canvas.drawBitmap(bmp1);//draws a JPG circle loaded from drawable resource
canvas.drawBitmap(bmp2);//draws a JPG square loaded from drawable resource
canvas.drawBitmap(bmp3);//draws a JPG triangle loaded from drawable resource

Can you visualize the result? No? Then imagine ;) ... continuing...

In the mobile screen I see 3 images mixed (circle, square and the triangle).


I question you...
does exist some method to save these 3 images in any SD Card directory? Something like:
canvas.saveWhatIsDrawnOnCanvasAsJpgInMySdCard();
or
WhateverUtil.saveWhatIsDrawnOnCanvasAsJpgInMySdCard(canvas);

any ideas???:thinking:
 
I am new to the world of Android and have been working on the same problem. In fact, I just wrote up something and put it on my blog page. I can't say that this is the best way, but it is one way I found that worked.

I defined a method in my view that takes the bitmap and writes it out as a jpeg file. The code looks something like:



public void saveAsJpg (File f)
{
String fname = f.getAbsolutePath ();
FileOutputStream fos = null;
try {
fos = new FileOutputStream (f);
mBitmap.compress (CompressFormat.JPEG, 95, fos);
Toast.makeText (getApplicationContext(), "Saved " + fname, Toast.LENGTH_LONG).show ();
} catch (Throwable ex) {
Toast.makeText (getApplicationContext(), "Error: " + ex.getMessage (), Toast.LENGTH_LONG).show ();
ex.printStackTrace ();
}
}


I also found that I had to do things with permissions in the manifest file. The full details are here: How to save jpeg files in the Android emulator.
 
  • Like
Reactions: garrydias
Upvote 0
Cool...
but I already got.
I used a new Canvas object with my own Bitmap object:

protected void onDraw(Canvas canvas){

Bitmap myBitmap = new Bitmap();
Canvas mycanvas = new Canvas(myBitmap);
mycanvas.draw(squareBitmap);//draw over mybitmap
mycanvas.draw(circleBitmap);//draw over mybitmap mixing with squareBitmap
mycanvas.draw(triangleBitmap);//draw over mybitmap mixing with circleBitmap+squareBitmap

canvas.draw(myBitmap);
}

It
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones