newbie: j2me - accessing resources in midlet

J

Jeff

Hey

midp 2.0
J2ME Wireless Toolkit 2.2
JDK 5

I've created a midlet using KToolbar, and placed a .png image in the <midlet
name>\ res folder...
(i haven't done anything else to add the image to the project, I rely on
KToolbar to do the rest when I package the midlet... maybe that is wrong og
me?)

When I run the code below on the emulator, the image is shown and everything
seems to be working... but it doesn't work on my mobile (nokia 6630).... I'm
not sure why but it looks like this test method don't return....

By the way am I using the correct technique for accessing resources in the
<midlet name>\ res folder? (I tryed using " image = Image.createImage
("/test.png");" without success)

I appreciate any tips that can help me solve my problem code below


public void test()
{
display = Display.getDisplay(midlet);
Image image;
try
{
InputStream is = getClass().getResourceAsStream("/test.png");
image = Image.createImage (is);
}
catch (IOException e)
{
throw new RuntimeException ("Unable to load Image: "+e.toString());
}
}

Jeff
 
J

Jeff

What do you mean with a jar directory?

I created this midlet with KToolbar and KToolbar created a directory (on my
hard drive c:\WTK22\... ) for this midlet, but it didn't create a sub-folder
named "jar".... (but it created many other sub-folders : "bin", "classes",
"lib", "res", "src", "tmpclasses", "tmplib")

I've just placed the image in the "res" sub folder and then created the
package. I mean I did choose " Project | Package | Create Package" in
KToolbar, I didn't do anything else for getting the image into the package

Jeff
 
R

Roedy Green

I've just placed the image in the "res" sub folder and then created the
package. I mean I did choose " Project | Package | Create Package" in
KToolbar, I didn't do anything else for getting the image into the package

In that case, lets find out what your classpath is. Dump it out the
system property.

System.out.println( System.getProperty( "java.class.path" ) );

Your resource had better be DIRECTLY in one on the classpath elements,
not buried in a subdir of it.

If you changed that to

InputStream is = getClass().getResourceAsStream("test.png");

Then you should have a file called com/yourdomain/test/test.png in
one of the classpath elements.

where the package for test is com.yourdomain.test
 
J

Jeff

When I run this code in the emulator:

System.out.println( System.getProperty( "java.class.path" ) );

it returns "null" (I run with the text "java.class.path" as argument)

When running the code on my emulator everything works fine, it shows the
image in the window... everything is okay when runnning from the emulator,
it's when I run this midlet on the mobile I get trouble with this image...
maybe it's because the midlet is unable to find the image in the package
when installed on the mobile...??
 
A

Andrew Thompson

Jeff said:
When I run this code in the emulator:

System.out.println( System.getProperty( "java.class.path" ) );

it returns "null" (I run with the text "java.class.path" as argument)

When running the code on my emulator everything works fine, it shows the
image in the window... everything is okay when runnning from the emulator,
it's when I run this midlet on the mobile I get trouble ..

I suspect this 'trouble' is security related.

An untrusted *Applet* will not show you the java.class.path
(unless it is run in the *MSVM* - which will tell you directly
where the core java classes are stored, no problem)
 
D

Darryl L. Pierce

Jeff said:
When I run the code below on the emulator, the image is shown and everything
seems to be working... but it doesn't work on my mobile (nokia 6630).... I'm
not sure why but it looks like this test method don't return....

Check that your JAR file has the image in it.
Image image;
try
{
InputStream is = getClass().getResourceAsStream("/test.png");
image = Image.createImage (is);
}
catch (IOException e)
{
throw new RuntimeException ("Unable to load Image: "+e.toString());
}
}

If you're loading the image from the JAR file, rather than from some
other source that only returns a stream, then you should just use:

Image image = Image.createImage("/test.png");
 
J

Jeff

I replaced this code:
InputStream is = getClass().getResourceAsStream("/test.png");
image = Image.createImage (is);

with this code
Image image = Image.createImage("/test.PNG");

And now it works on both emulator and mobile phone

I'm very thankful for all the help you guys have given me on this subject.
 
D

Darryl L. Pierce

Jeff said:
I replaced this code:
InputStream is = getClass().getResourceAsStream("/test.png");
image = Image.createImage (is);

with this code
Image image = Image.createImage("/test.PNG");

And now it works on both emulator and mobile phone

I'm very thankful for all the help you guys have given me on this subject.

You're welcome, mate. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top