Blackberry OS (4.3 and above) provides a very easy way to capture screen of device. We can use screenshot(Bitmap bmp) method of Display class. In this post you will see how to capture screen contents and save to a file on SDCard.
First we will get width and height of screen. This is very important because Bitmap we pass to screenshot method must have same width and height as of screen.
First we will get width and height of screen. This is very important because Bitmap we pass to screenshot method must have same width and height as of screen.
int screenWidth=Display.getWidth();Next, create a Bitmap object of dimensions equivalent to width and height of screen.
int screenHeight=Display.getHeight();
Bitmap bmpScreen=new Bitmap(screenWidth,screenHeight);Take screenshot using screenshot method of Display class
Dispaly.screenshot(bmpScreen);Now bmpScreen contains our screen image data. Our next task is to save this data in a file on SDCard. Let's create PNGEncodedImage from this Bitmap.
PNGEncodedImage imgToSave=PNGEncodedImage.encode(bmpScreen);Open a file to write this image
FileConnection fc=(FileConnection)Connector.open("file:///SDCard/screenshot.png");Get DataOutputStream of FileConnection and write data of our encoded image. PNGEncodedImage's getData method will return raw bytes of image.
fc.create();
DataOutputStream oStream=fc.openDataOutputStream();
oStream.write(imgToSave.getData());
oStream.close();
fc.close();
Comments
Post a Comment
Share your wisdom