Yatin
Yatin

Reputation: 21

Loading images in j2me

I got a problem with loading images in j2me
Where I can upload some type of images for j2me emulator but for some it does not load
So, what's the problem? means what type of images can be loaded in class extending Canvas class? should there be specific image size or type to be uploaded?

Upvotes: 1

Views: 787

Answers (2)

hasanghaforian
hasanghaforian

Reputation: 14042

I guess the problem is one of the following reasons:

1.Resource name:

Resource names are case-sensitive; for example, /ellen-png.png is not the same resource as /ellen-png.PNG or any other variation.

2.Format:

The only format that must be supported is png. Some phones might support other formats such as:

  • Graphics Interchange Format (GIF)
  • Joint Photographic Experts Group (JPEG, JPG)
  • Windows or OS/2 Bitmap (BMP)
  • Tag Image File Format (TIF)
  • PC Paintbrush bitmap (PCX)
  • Raw signed PCM data (RAW)

But that depends on the device, and if you wanna be as cross-platform as possible then just use pngs:
In 1999, when Sun Microsystems developed the J2ME platform specification and its supporting APIs, PNG was chosen as the default image format because of its graphics capabilities, small file size, and that it was unencumbered with patent issues.
To determine if J2ME handles a specific media format, you invoke the getSupportedContentTypes method. This method returns a string array of supported media types, and includes the audio and video formats along with the image formats. The strings present this information in MIME-type format.For example to check for GIF support, you scan this array, looking for the GIF MIME-type string. If there is a match, then the phone's J2ME implementation supports native display of GIF files.This snippet code can use for this purpose:

private String   mediaTypes[];   
private final    String GIF_MIME_TYPE = "image/gif";   
private boolean  gifSupported;    

// Get the media types to check for support of GIF file display   
mediaTypes = Manager.getSupportedContentTypes(null);   
int count = mediaTypes.length;   

// Check list for GIF MIME type; set support flag if present   
gifSupported = false;   
for (int i = 0; i < count; i++) {   
    if (mediaTypes[i] == GIF_MIME_TYPE)   
        gifSupported = true;   
} // end for   

If you would to use special format,you can use special decoders to decode image from not supported format.
createImage throws java.io.IOException if the resource does not exist(specially incorrect resource name), the data cannot be loaded, or the image data cannot be decoded.So if you catch this exception check every case.

3.Size:
Note that the sizes of JAR files that specific devices can load depend on multiple factors, including limitations imposed by handset manufacturers and carriers, or even the available memory configuration on a specific device.Image file sizes must be in range 9.1 Kbytes for GIF to 69.5 Kbytes for TIF.Of course, since you probably want to make them as small as possible, you might want to try a tool like pngcrush, for that. Its main purpose is to reduce the size of the PNG IDAT datastream by trying various compression levels and PNG filter methods.

References:

SDN FAQ - What image types does MIDP support?
gamedev.net - Image Formats in J2ME
drdobbs.com - Displaying GIF Images on J2ME Mobile Phones

Upvotes: 1

Altaaf
Altaaf

Reputation: 527

I am not sure exactly what you are looking for, but the behavior you describe very much sounds like you are experiencing an OutOfMemory exception. Try reducing the dimensions of your images (heap usage is based on dimension) and see if the behavior ceases. This will let you know if it is truly an OutOfMemory issue or something else.

Other tips:

1) Load images largest to smallest. This helps with heap fragmentation and allows the largest heap space for the largest images. 2) Unload (set to null) in reverse order of how you loaded and garbage collect after doing so. Make sure to Thread.yield() after you call the GC. 3) Make sure you only load the images that you need. Unload images from a state that the application is no longer in. 4) Since you are creating sprites you may have multiple sprites for one image. Consider creating an image pool to make sure you only load the image once. Then just point each Sprite object to the image within the pool that it requires. Your example in your question seems like you would more than likely load the same image into memory more than once. That's wasteful and could be part of the OutOfMemory issue.

Upvotes: 1

Related Questions