This article applies to the following:
- BlackBerry® Device Software 4.3 and later
- BlackBerry smartphones
The following code sample demonstrates how to capture and save a screen shot within a BlackBerry smartphone application.
Note: The sample code should be executed in its own thread because it performs IO operations.
A complete sample application can be downloaded here.
//Get the dimensions of the screen.
int width = Display.getWidth();
int height = Display.getHeight();
//Create a bitmap to store the screen capture in.
Bitmap bm = new Bitmap(width, height);
Display.screenshot(bm);
//Convert the Bitmap object to a PNG.
PNGEncodedImage png = PNGEncodedImage.encode(bm);
//Save the PNG to the micro SD card.
FileConnection fc = (FileConnection)Connector.open("file:///SDCard/screenCap.png");
//This sample overwrites the file. Delete the file if it exists.
if (fc.exists())
{
fc.delete();
}
//Create the file.
fc.create();
//Write out the data to the file.
DataOutputStream out = fc.openDataOutputStream();
out.write(png.getData());
//Close the Connections.
out.close();
fc.close();