Saving Canvas drawing as JPG/PNG/etc..


Last Updated:

  1. garrydias

    garrydias Member This Topic's Starter

    Joined:
    Aug 9, 2010
    Messages:
    11
    Likes Received:
    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???:confused:
     

    Advertisement
  2. blahti

    blahti New Member

    Joined:
    Nov 19, 2010
    Messages:
    1
    Likes Received:
    1
    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.
     
    garrydias likes this.
  3. garrydias

    garrydias Member This Topic's Starter

    Joined:
    Aug 9, 2010
    Messages:
    11
    Likes Received:
    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
     

Share This Page

Loading...