Monday, December 8, 2008

Saving a view as an image with Android

Our Android application PinPoint can send map images via MMS and email. We used the following technique to be able to convert the MapView into an image that could be used across applications. 

Firstly create a Bitmap object with the appropriate width and height of your view, along with a configuration. RGB_565 seems to work just fine for the MapView canvas.

Bitmap image = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.RGB_565);

Next create a canvas with the bitmap and pass that into the draw() method of the view. This will ask the view to draw it's contents onto the canvas and therefore the associated bitmap.

view.draw(new Canvas(image));

Next insert the image into the Media library. This returns a URL that refers to the stored image and can be reused across applications. In our case we could pass this URL to the MMS application to have it embed the image in a message.  I'll post some sample code of how to send this image via MMS shortly.

String url = Images.Media.insertImage(getContentResolver(), image, "title", null);

To test this in the emulator you'll need to setup an external storage device otherwise you'll see an error like :-
java.lang.UnsupportedOperationException: Unknown URI: content://media/external/images/media

First you'll need to create a storage image using the mksdcard from the tools folder of the emulator
mksdcard 10M sdcard.iso

Then when you start the emulator you need to pass the -sdcard parameter 
-sdcard /sdcard.iso

In my next post I'll write up how to send this image via MMS


guersel said...

great work, but it doesn't work for me; i want to save a table on a bitmap, but before and after calling: table.draw(new Canvas(bitmap)) the table has width and height = 0!? The bitmap is mutable and i set all layoutparams attributes on any childs of the table additionally the table has the right count of childs..
Any ideas?

thank you

el_seby said...


I use the next code for getting a bitmap from a LinearLayout and then use it as a texture with OpenGL ES...but when I put the texture it is all black. If I modified the bitmap manually it work ok... it seems like draw function does nothing :S

What I'm doing bad??


LinearLayout lay = new LinearLayout(context); lay.setBackgroundColor(0x00ffffff);

TextView tv = new TextView(context); tv.setBackgroundColor(0x00ccccff);
tv.setText("Río Rosas");
tv.layout(0, 0, 200, 200);
lay.addView(tv, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
Bitmap bitmap = null;

bitmap = Bitmap.createBitmap(200, 200,Bitmap.Config.ARGB_8888);
//bitmap.eraseColor(0xffffffff); //bitmap.setPixel(50, 50, 0xffffffff);

canvas = new Canvas(bitmap);

ugurtuhafiye said...

Thanks for your post. I am trying to implement mapview to bitmap image algorithm but i get the unresolve mapview as image. I wrote the code in this link..

joy said...

Thanks for sharing such a wonderful article, I hope you could inspire more people. Visit my site too.