views:

2780

answers:

4

Hi All,

I tried this on J2ME

try {
    Image immutableThumb = Image.createImage( temp, 0, temp.length);
} catch (Exception ex) {
    System.out.println(ex);
}

I hit this error: java.lang.IllegalArgumentException:

How do I solve this?

+1  A: 

Image.createImage() throws an IllegalArgumentException if the first argument is incorrectly formatted or otherwise cannot be decoded. (I'm assuming that temp is a byte[]).

http://java.sun.com/javame/reference/apis/jsr118/javax/microedition/lcdui/Image.html#createImage(byte[],%20int,%20int)

(This URL refuses to become a hyperlink for some reason (?))

tjlevine
+1  A: 

It's hard to say without more details or more surrounding code, but my initial suspicion is that the file your are trying to load is in a format not supported by the device.

izb
A: 

Let us have a look at the docs: IllegalArgumentException is thrown

if imageData is incorrectly formatted or otherwise cannot be decoded

So the possible reason can be either unsupported format of the image, or truncated data. Remember, you should pass entire file to that method, including all the headers. If you have doubts about the format, you'd better choose PNG, it must be supported anyway.

Malcolm
A: 

I just had the same problem with my MIDLET and the problem in my case was the HTTP header that comes along the JPEG image that I read from the socket's InputStream. And I solved it by finding the JPEG SOI marker that is identified by two bytes: FFD8 in my byte array. Then when I find the location of the FFD8 in my byte array, I trim the starting bytes that represent the HTTP header, and then I could call createImage() without any Exception being thrown...

You should check if this is the case with you. Just check is this true (temp[0] == 0xFF && temp[1] == 0xD8) and if it is not, trim the start of temp so you remove HTTP header or some other junk...

P.S. I presume that you are reading JPEG image, if not, look for the appropriate header in the temp array.

Also if this doesn't help, and you are reading JPEG image make sure that the array starts with FFD8 and ends with FFD9 (which is the EOI marker). And if it doesn't end with the EOI just trim the end like I explained for SOI...

P.P.S And if you find that the data in temp is valid, then your platform cannot decode the JPEG images or the image in temp is to large for JPEG decoder.

Cipi