Creating image from current View. How To?

2.4k views Asked by At

My application is some kind of "mini paint" and I would like to save my current view to the device memory... I would like to do the the opposite process too (Load an image from the device memory and set it as my currentview)

MiniPaint

yeah that's suppose to be a flamingo, I'm an artist!

2

There are 2 answers

1
Jimmy Theis On

Haven't tried it myself, but this answer shows taking a screenshot programmatically by getting the root view and saving off its drawing cache. That may be all you need to save your painting.

EDIT: Fixed link

1
Ben P On

First off I am assuming you are performing this drawing by overriding the onDraw() method on a View object, which passes in a Canvas object that you then perform some drawing operations on.

Here's a very basic way to approach this problem. There are probably lots of additional considerations to take into account, such as the file format(s) you read from and write to, and some extra error handling in the I/O code. But this should get you going.

To save what drawing you currently have, write out your View's drawingCache to a Picture object, then use the Picture's writeToStream method.

To load a pre-existing picture, you can use the Picture.readFromStream method, then in your onDraw call, draw the loaded picture to your Canvas.

To Wit:

/**
 * Saves the current drawing cache of this View object to external storage.
 * @param filename a file to be created in the device's Picture directory on the SD card
 */
public void SaveImage(String filename) {
    // Grab a bitmap of what you've drawn to this View object so far
    Bitmap b = this.getDrawingCache();
    // It's easy to save a Picture object to disk, so we copy the contents
    // of the Bitmap into a Picture
    Picture pictureToSave = new Picture();
    // To copy the Bitmap into the Picture, we have to use a Canvas
    Canvas c = pictureToSave.beginRecording(b.getWidth(), b.getHeight());
    c.drawBitmap(b, 0, 0, new Paint());
    pictureToSave.endRecording();

    // Create a File object where we are going to write the Picture to
    File file = new File(this.getContext().getExternalFilesDir(Environment.DIRECTORY_PICTURES), filename);
    try {
        file.createNewFile();
    }
    catch (IOException ioe) {
        ioe.printStackTrace();
    }
    // Write the contents of the Picture object to disk
    try {
        OutputStream os = new FileOutputStream(file);
        pictureToSave.writeToStream(os);
        os.close();
    }
    catch (FileNotFoundException fnfe) {
        fnfe.printStackTrace();
    }
}

/**
 * Returns a Picture object loaded from external storage
 * @param filename the name of the file in the Pictures directory on the SD card
 * @return null if the file is not found, or a Picture object.
 */
public Picture LoadImage(String filename) {
    // Load a File object where we are going to read the Picture from
    File file = new File(this.getContext().getExternalFilesDir(Environment.DIRECTORY_PICTURES), filename);
    Picture pictureToLoad = null;
    try {
        InputStream is = new FileInputStream(file);
        pictureToLoad = Picture.createFromStream(is);
        is.close();
    }
    catch (FileNotFoundException fnfe) {
        fnfe.printStackTrace();
    }
    // Return the picture we just loaded. Draw the picture to canvas using the
    // Canvas.draw(Picture) method in your View.onDraw(Canvas) method
    return pictureToLoad;
}

Useful links I read to figure this out: