0

I have a problem with the next class:

public class MyView extends SurfaceView implements Runnable {

    public void run() {
        Canvas c = holder.lockCanvas();
        c.drawARGB( 255, 0, 255, 80 );
        for ( short i = 0; i < bitmapsArr.size(); i++ ) {
            c.drawBitmap( bitmap.get( i ), 100, 50, null );
        }
        holder.unlockCanvasAndPost( c );
    }

    public void saveCanvasToFile( String filename ){

        // ???

    }

}

I draw on canvas which places on another thread. I wanna save this canvas to file but I don't know how to do it. I've tried to use the DrawingCache system - but it doesn't work cause my drawings on other thread... Doest anybody know what to do in this case?

Loktar
  • 34,764
  • 7
  • 90
  • 104
JavaRunner
  • 2,455
  • 5
  • 38
  • 52
  • 1
    Create the `Bitmap` using [this method](http://stackoverflow.com/a/4013780/1438733), then save it using [this method](http://stackoverflow.com/a/673014/1438733). – Cat Oct 31 '12 at 23:58
  • do not put "Andoid" in subject. You got tags for this – Marcin Orlowski Oct 31 '12 at 23:59
  • @Eric, but how I can do it? How can I execute "Canvas(bitmap)" when I already have this canvas? :) – JavaRunner Nov 01 '12 at 00:02
  • @JavaRunner You create a new `Canvas`, assign the `Bitmap` to it, and then execute the same code as you do on this one, with that `Canvas` instead. Edit: Oh, apparently you figured that out before I posted this. Congrats! :) – Cat Nov 01 '12 at 01:59

1 Answers1

0

I found a solution. Actually you need to redraw all your drawings into new canvas. Example:

Bitmap bitmap = Bitmap.createBitmap( this.getWidth(), this.getHeight(), Bitmap.Config.ARGB_8888 );
Canvas c1 = new Canvas( bitmap );
c1.drawARGB( 255, 0, 255, 80 );
for ( short i = 0; i < bitmapsArr.size(); i++ ) {
    c1.drawBitmap( bitmap.get( i ), 100, 50, null );
}
// and then you can save your bitmap with:
// bitmap.compress( Bitmap.CompressFormat.PNG, 90, fileoOutputStream );
JavaRunner
  • 2,455
  • 5
  • 38
  • 52