Encode an image

  1. Import the required classes.
    import net.rim.device.api.system.EncodedImage;
  2. Invoke EncodedImage.createEncodedImage(). This method uses the unprocessed image data in the byte array to create an instance of EncodedImage.
  3. 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.
    // Store the contents of the image file. 
    private byte[] data = new byte[2430]; 
    try {
        // 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."); 

Was this information helpful? Send us your comments.