Are there any chances that I could resize an image using the fileconnection inputstream?

FileConnection fc = (FileConnection) Connector.open("file:///" + filePath + "/",Connector.READ);
InputStream fis = (InputStream) fc.openInputStream();

I want to resize the image from here because if I use

Image.createImage(fis);

I am encountering "Out of Memory error", if the file size is too large.

What I wanna do is have a function similar to resize(fis, percent) before using Image.createImage(fis) so that image is resized before creating it.

Dani AI

Generated

Short answer: the standard MIDP Image API does not provide a way to decode-and-scale an image directly from an InputStream — Image.createImage(fis) will decode the whole image into memory. That explains the OutOfMemoryError reported by . The routine posted by is a good in-memory resizer but still requires the full source Image to exist first, so it does not avoid the initial memory spike.

Practical options (ordered by reliability):

  • Serve or ship smaller images if possible — generate thumbnails on the server or at capture time and fetch those instead of the full file.
  • When the app controls capture, request smaller capture resolution (device-dependent).
  • Inspect image metadata (file headers) before allocating memory and only load images that are already small enough; if the header shows a huge image, fetch or create a smaller version instead of attempting to decode it on-device.
  • If a native/vendor API on the target device supports decoder subsampling or streaming decode, prefer that — these are vendor-specific and not part of standard MIDP.

A compact utility to read JPEG dimensions without decoding the full image (read a small header block, then close the stream and reopen if the image will be loaded) — this lets the app decide whether to load or skip:

public static int[] readJpegDimensions(InputStream in) throws IOException {
    int a = in.read(), b = in.read();
    if (a != 0xFF || b != 0xD8) return null; // not JPEG
    while (true) {
        int prefix = in.read();
        while (prefix != 0xFF) {
            if (prefix == -1) return null;
            prefix = in.read();
        }
        int marker;
        do { marker = in.read(); if (marker == -1) return null; } while (marker == 0xFF);
        if (marker == 0xDA || marker == 0xD9) return null; // SOS or EOI
        int len = (in.read() << 8) + in.read();
        if ((marker >= 0xC0 && marker <= 0xC3) ||
            (marker >= 0xC5 && marker <= 0xC7) ||
            (marker >= 0xC9 && marker <= 0xCB) ||
            (marker >= 0xCD && marker <= 0xCF)) {
            in.read(); // precision
            int height = (in.read() << 8) + in.read();
            int width  = (in.read() << 8) + in.read();
            return new int[] { width, height };
        }
        // skip segment
        for (int i = 0; i < len - 2; i++) if (in.read() == -1) return null;
    }
}

Additional tips: check available heap (Runtime.getRuntime().freeMemory()), null out references to large images and call System.gc() before loading a new image, catch OutOfMemoryError around heavy loads, and prefer loading only images sized to the device screen. When memory is constrained, the most robust fix is to avoid decoding full-size images on the device in the first place.

Originally this code is developed by Mr. Heriman. I hope this will help you.

public static Image resizeImage(Image src, int screenWidth, int screenHeight)
{
    int srcWidth = src.getWidth();
    int srcHeight = src.getHeight();
    Image tmp = Image.createImage(screenWidth, srcHeight);
    Graphics g = tmp.getGraphics();
    int ratio = (srcWidth << 16) / screenWidth;
    int pos = ratio / 2;

    // Horizontal Resize
    for (int x = 0; x < screenWidth; x++)
    {
        g.setClip(x, 0, 1, srcHeight);
        g.drawImage(src, x - (pos >> 16), 0, Graphics.LEFT | Graphics.TOP);
        pos += ratio;
    }

    Image resizedImage = Image.createImage(screenWidth, screenHeight);
    g = resizedImage.getGraphics();
    ratio = (srcHeight << 16) / screenHeight;
    pos = ratio / 2;

    //Vertical resize
    for (int y = 0; y < screenHeight; y++) {
        g.setClip(0, y, screenWidth, 1);
        g.drawImage(tmp, 0, y - (pos >> 16), Graphics.LEFT | Graphics.TOP);
        pos += ratio;
    }

    return resizedImage;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.