Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2005

Tip: Looking for answers? Try searching our database.

newbie: j2me - accessing resources in midlet

Thread view: 
Jeff - 14 Oct 2005 16:57 GMT
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
Roedy Green - 15 Oct 2005 04:20 GMT
>   InputStream is = getClass().getResourceAsStream("/test.png");
>   image = Image.createImage (is);

what does your jar directory look like?
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

Jeff - 15 Oct 2005 08:35 GMT
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

>>   InputStream is = getClass().getResourceAsStream("/test.png");
>>   image = Image.createImage (is);
>
> what does your jar directory look like?
Roedy Green - 15 Oct 2005 09:00 GMT
>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
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

Jeff - 15 Oct 2005 09:35 GMT
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...??

>>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
[quoted text clipped - 16 lines]
>
> where the package for test is com.yourdomain.test
Andrew Thompson - 15 Oct 2005 11:06 GMT
> When I run this code in the emulator:
>
[quoted text clipped - 5 lines]
> 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)
Darryl L. Pierce - 15 Oct 2005 14:35 GMT
> System.out.println( System.getProperty( "java.class.path" ) );

That's an undefined property in the MIDP.

Signature

Darryl L. Pierce <mcpierce@gmail.com>
Homepage: http://mcpierce.multiply.com/
"Bury me next to my wife. Nothing too fancy..." - Ulysses S. Grant

Darryl L. Pierce - 15 Oct 2005 14:34 GMT
> 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
[quoted text clipped - 7 lines]
>   }
> }

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");

Signature

Darryl L. Pierce <mcpierce@gmail.com>
Homepage: http://mcpierce.multiply.com/
"Bury me next to my wife. Nothing too fancy..." - Ulysses S. Grant

Jeff - 15 Oct 2005 15:40 GMT
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.

>> 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
[quoted text clipped - 20 lines]
>
> Image image = Image.createImage("/test.png");
Darryl L. Pierce - 15 Oct 2005 16:09 GMT
> I replaced this code:
> InputStream is = getClass().getResourceAsStream("/test.png");
[quoted text clipped - 6 lines]
>
> I'm very thankful for all the help you guys have given me on this subject.

You're welcome, mate. :)

Signature

Darryl L. Pierce <mcpierce@gmail.com>
Homepage: http://mcpierce.multiply.com/
"Bury me next to my wife. Nothing too fancy..." - Ulysses S. Grant



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.