J2ME中的Image.createImage问题
我在 J2ME 上尝试了这个,
try {
Image immutableThumb = Image.createImage( temp, 0, temp.length);
} catch (Exception ex) {
System.out.println(ex);
}
但遇到了这个错误: java.lang.IllegalArgumentException:
我该如何解决这个问题?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果第一个参数的格式不正确或无法解码,Image.createImage() 会抛出 IllegalArgumentException。 (我假设 temp 是一个 byte[])。
http://java.sun.com/javame/reference/apis/jsr118/javax/microedition/lcdui/Image.html#createImage(byte[],%20int,%20int)
(此网址由于某种原因拒绝成为超链接(?))
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 (?))
如果没有更多细节或更多周围代码,很难说,但我最初怀疑您尝试加载的文件采用了设备不支持的格式。
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.
让我们看一下文档:抛出 IllegalArgumentException
则可能的原因可能是图像格式不受支持或数据被截断。 请记住,您应该将整个文件传递给该方法,包括所有标头。 如果你对格式有疑问,最好选择PNG,无论如何它都必须支持。
Let us have a look at the docs: IllegalArgumentException is thrown
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.
我的 MIDLET 也遇到了同样的问题,我的例子中的问题是我从套接字的 InputStream 读取的 JPEG 图像附带的 HTTP 标头。 我通过在字节数组中找到由两个字节标识的 JPEG SOI 标记来解决这个问题:
FFD8
。 然后,当我在字节数组中找到FFD8
的位置时,我会修剪表示 HTTP 标头的起始字节,然后我可以调用createImage()
而不会出现任何异常被扔...你应该检查一下你是否遇到这种情况。 只需检查
(temp[0] == 0xFF && temp[1] == 0xD8)
是否为真,如果不是,则修剪temp
的开头所以你删除 HTTP 标头或其他一些垃圾...PS
我假设您正在读取 JPEG 图像,如果不是,请在
temp
数组中查找适当的标头。另外,如果这没有帮助,并且您正在读取 JPEG 图像,请确保数组以
FFD8
开头并以FFD9
结尾(这是 EOI 标记)。 如果它没有以 EOI 结尾,只需修剪结尾,就像我为 SOI 解释的那样......PPS
如果您发现
temp
中的数据有效,则说明您的平台无法解码 JPEG 图像,或者temp
中的图像对于 JPEG 解码器来说太大。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 theFFD8
in my byte array, I trim the starting bytes that represent the HTTP header, and then I could callcreateImage()
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 oftemp
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 withFFD9
(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 intemp
is to large for JPEG decoder.