×

Discussion Board

Results 1 to 10 of 10
  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    12

    IOException when loading an image

    I have been trying to use:

    String fileLocation = "database/H1/S1/image.png";
    try{
    Image image = Image.createImage(fileLocation);
    }catch{
    ...

    to load an image file but I constantly get an IOException. The fileLocation is definitely correct as I have previously loaded text files from the same path, and I can see that the image.png definitely exists.

    The documentation states:
    public static Image createImage(String name) throws IOException
    Throws IOException - if the resource does not exist, the data cannot be loaded, or the image data cannot be decoded

    The next step would be to check these conditions but I have no idea how to do a fileexists type check in JavaME etc.

    Any help would be appreciated.

    Cheers.

  2. #2
    Registered User
    Join Date
    Dec 2005
    Location
    Brazil
    Posts
    1,884

    Re: IOException when loading an image

    Hi,

    Not sure if you are talking about device or emulator, tipically you should use something like "/res/image.png" or event "/image.png".

    http://www.developer.com/java/j2me/p...0934_1561591_6

    Have you checked the final jar file and make sure the location (root + package names) is correct?

    It is trivial task load images with Image.createImage so double check your code and actual file location (must be visible classpath).
    Juarez Alvares Barbosa Junior - Brazil

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    12

    Re: IOException when loading an image

    I'm developing in Eclipse for JavaME using the Java Wireless Toolkit and the EclipseME plugin.

    I'm accessing files as relative paths from the src root which has worked perfectly when I've been creating files previously.

    For example, one line above trying to createImage, I read in a .txt file from the same directory that image.png is in by using "database/H1/S1/text.txt". I can't understand why there would be a problem access the image in the same directory.

    I don't know what "checking" the final JAR file means. As far as my knowledge goes, the JAR file is simply the packaged class files that will be eventually put on the mobile phone. I've purely been working with java files in my local directory on my development computer.

    The class path must be correct surely, otherwise I would not have been able to open the text file within the same directory?

  4. #4
    Super Contributor
    Join Date
    Apr 2003
    Location
    USA, CA
    Posts
    7,191

    Re: IOException when loading an image

    That locator might work in the emulator for some things, but you should not really use those PC directories. You should get the correct location from the emulator.
    The locators should have both "the drive" and the file folder specified

    Some instructions for using the file system from here
    http://www.forum.nokia.com/document/...rs_Library_v1/
    http://www.forum.nokia.com/info/sw.n..._0_en.zip.html

    Hartti

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    8

    Re: IOException when loading an image

    You must examine all possibilities.

    1. check the PNG image to make sure it is a PNG image. Don't trust the file extension, open the file in a text editor to verify its contents.

    2. Use an absolute path. "/database/H1/S1/image.png" not "database/H1/S1/image.png"

    3. Open a simple PNG image at the root path and see if that works, "/image.png"

  6. #6
    Registered User
    Join Date
    Feb 2008
    Posts
    12

    Re: IOException when loading an image

    Quote Originally Posted by hartti View Post
    That locator might work in the emulator for some things, but you should not really use those PC directories. You should get the correct location from the emulator.
    The locators should have both "the drive" and the file folder specified
    I eventually want to just dump the JAR file onto various kinds of phone and run it. How can I determine what the correct location should be, if I do not know where the user will store the JAR file? I assumed that relative paths would stay intact within the JAR file.

    Quote Originally Posted by jonathanle View Post
    You must examine all possibilities.

    1. check the PNG image to make sure it is a PNG image. Don't trust the file extension, open the file in a text editor to verify its contents.

    2. Use an absolute path. "/database/H1/S1/image.png" not "database/H1/S1/image.png"

    3. Open a simple PNG image at the root path and see if that works, "/image.png"
    I can confirm that it is definitely a PNG file. It's actually a duplicate of an image file that I have opened before using createImage. Changing to an absolute path has no change in effect. Moving to, and trying to open it from the root directory also has no change in effect (still IOException error).

    Would it help if I pasted some code, and directory structure?

  7. #7
    Registered User
    Join Date
    Feb 2008
    Posts
    12

    Re: IOException when loading an image

    Problem solved. I misunderstood createImage and thought that it took the path of an image.

    I have to do this:
    InputStream imageSource = getClass().getResourceAsStream(fileLocation);
    image = Image.createImage(imageSource);

    Cheers for the help, folks.

  8. #8
    Regular Contributor
    Join Date
    Apr 2007
    Posts
    220

    Re: IOException when loading an image

    Hi There
    "checking" the final JAR file means you can extract the jar fine by using any extractor like win-zip/win-rar and see whether the resources have actually been packaged or not?

    Regards

  9. #9
    Registered User
    Join Date
    Feb 2008
    Posts
    12

    Re: IOException when loading an image

    Quote Originally Posted by amitmishra19 View Post
    Hi There
    "checking" the final JAR file means you can extract the jar fine by using any extractor like win-zip/win-rar and see whether the resources have actually been packaged or not?

    Regards
    Ah right, cool. Sorry I thought that there was maybe more to it. The JAR file is good.

    -------------

    I'm trying to import a .mp3 file now using the same idea but I'm hitting a MediaException. I'm using:

    InputStream audioSource = getClass().getResourceAsStream(fileLocation);
    Player audio = Manager.createPlayer(audioSource,"audio/mp3");

    Can anyone spot any faults / things I should be doing different?

  10. #10
    Regular Contributor
    Join Date
    Apr 2007
    Posts
    220

    Re: IOException when loading an image

    Hi,

    please check the supported MIME type for the mp3 files in your phone....it may be audio/mp3, audio/mpeg or other.

    Regards.

Similar Threads

  1. Opening a JPEG Image
    By ummarbhutta in forum Mobile Java Media (Graphics & Sounds)
    Replies: 8
    Last Post: 2007-02-15, 06:34
  2. how to cut some part of Image
    By mshouab in forum Mobile Java Media (Graphics & Sounds)
    Replies: 2
    Last Post: 2006-08-04, 09:05
  3. Nokia Image Converter
    By davidpurdie in forum General Development Questions
    Replies: 0
    Last Post: 2004-02-18, 15:31
  4. Loading Image data from 'raw' bytes
    By LongSteve in forum Mobile Java General
    Replies: 2
    Last Post: 2002-11-20, 17:38

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×