Visit Sponsor

Written by 6:16 pm Core Java

Save Bitmap Image in BlackBerry Java

Overview

This post explains how to save a bitmap image file to the filesystem in BlackBerry Java applications. The example uses BlackBerry’s native APIs to encode a Bitmap object into PNG format and write it to device storage. This approach is common when building legacy BlackBerry Java apps requiring image persistence.

BlackBerry Java uses classes such as PNGEncodedImage, FileConnection, and the system graphics APIs. It’s important to handle file I/O and encoding properly to avoid errors and ensure compatibility across devices.

Concepts

Bitmap and EncodedImage

  • Bitmap represents raw pixel data.
  • PNGEncodedImage encodes a Bitmap into PNG format (lossless compression).
    • BlackBerry devices include APIs to encode to PNG using PNGEncodedImage.encode().

File Connections

  • BlackBerry uses javax.microedition.io.file.FileConnection (JSR-75) to access the filesystem.
  • Ensure proper permissions are granted for file read/write.

Saving a Bitmap Image

The following example shows how to save a bitmap as a PNG file on the BlackBerry filesystem (such as SDCard or device storage).

Steps

  1. Create or obtain a Bitmap object.
  2. Encode bitmap data as PNG using PNGEncodedImage.
  3. Open a file using FileConnection.
  4. Write the encoded byte data to the output stream.
  5. Close all resources properly.

Example Code

import java.io.OutputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.PNGEncodedImage;

public class ImageSaver {

    public static boolean saveBitmapToFile(String path, Bitmap bitmap) {
        FileConnection fileConn = null;
        OutputStream out = null;

        try {
            // Open or create the file
            fileConn = (FileConnection) Connector.open(path, Connector.READ_WRITE);
            if (!fileConn.exists()) {
                fileConn.create();
            }

            // Encode bitmap to PNG bytes
            PNGEncodedImage pngImage = PNGEncodedImage.encode(bitmap);
            byte[] imageData = pngImage.getData();

            // Open output stream and write data
            out = fileConn.openOutputStream();
            out.write(imageData);
            out.flush();

            return true;

        } catch (Exception e) {
            System.out.println("Error saving bitmap: " + e.toString());
            return false;

        } finally {
            try {
                if (out != null) out.close();
                if (fileConn != null) fileConn.close();
            } catch (Exception ignored) {}
        }
    }
}

Usage Example

Bitmap myBitmap = Bitmap.getBitmapResource("example.png");
String savePath = "file:///SDCard/BlackBerry/pictures/saved_image.png";

boolean success = ImageSaver.saveBitmapToFile(savePath, myBitmap);
if (success) {
    System.out.println("Image saved to " + savePath);
}

File Paths

Common storage paths on BlackBerry devices:

  • Internal memory:
    file:///store/home/user/…
  • SDCard storage:
    file:///SDCard/BlackBerry/pictures/…

Always confirm the target path exists and the application has appropriate permissions.

Error Handling and Best Practices

  • Handle exceptions when opening file connections and writing data.
  • Use try/finally to ensure streams and connections are closed.
  • Avoid writing large images on UI thread — run this code in a background thread if needed.
  • Always check that bitmap is not null before encoding.

Summary

This document shows how to encode and save a Bitmap as a PNG image file on legacy BlackBerry devices using Java APIs such as PNGEncodedImage and FileConnection. While the BlackBerry platform is legacy, understanding this pattern remains useful when maintaining enterprise applications built on BlackBerry Java.

Visited 12 times, 1 visit(s) today
Close