Troubleshooting guide
46
BlackBerry Java Development Environment Development Guide
Use encoded images
Task Steps
Access an image. 1. Save an image to the project folder or subfolder.
2. Add the image to the project in the BlackBerry® Integrated Development Environment.
3. Invoke Class.getResourceAsStream() to retrieve the image as an input stream of bytes.
private InputStream input;
...
try {
input = Class.forName("com.rim.samples.docs.imagedemo.ImageDemo").
getResourceAsStream("/images/example.png");
} catch (ClassNotFoundException e) {
System.out.println("Class not found");
}
Encode an image. 1. Invoke EncodedImage.createEncodedImage(). This method creates an instance of EncodedImage
using the raw image data in the byte array.
2. Check for an IllegalArgumentException, which EncodedImage.createEncodedImage()
throws if the byte array that you provide as a parameter does not contain a recognized image format.
private byte[] data = new byte[2430]; // Store the contents of the image file.
try {
input.read(data); // Read the image data into the byte array.
} catch (IOException e) {
// Handle exception.
}
try {
EncodedImage image = EncodedImage.createEncodedImage(data, 0, data.length);
} catch (IllegalArgumentException iae) {
System.out.println("Image format not recognized.");
}
Display an encoded image. 1. To assign the encoded image to a BitmapField, invoke BitmapField.setImage().
BitmapField field = new BitmapField();
field.setImage(image);
2. To add the BitmapField to the screen, invoke add().
add(field);
Set the decoding mode. 1. Invoke EncodedImage.setDecodeMode().
2. Provide one of the following modes as a parameter to the method:
• DECODE_ALPHA: decodes an alpha channel, if one exists (this is the default mode)
• DECODE_NATIVE: forces the application to decode the bitmap to the native bitmap type of the
BlackBerry device software application
• DECODE_READONLY: marks the decoded bitmap as read-only
Set the image display size. >Invoke EncodedImage.setScale().
The inverse of the integer that the scale parameter specifies scales the image.
For example, if you set the scaling factor to 2, the image decodes at 50% of
its original size.