×

Discussion Board

Results 1 to 3 of 3
  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    2

    load image problem

    I'm having a problem loading an image from my nokia n95, I think it has something to do with the location of the file, the code I'm using is taken from http://www.java2s.com/Code/Java/J2ME/ImageMIDlet.htm

    I only altered the path to image from an image inside the application to an image on my phone's file directory.
    And I'm getting the error 'Failed to load image' as specified by the caught IOException.
    The code I'm using for drawing the image is :

    Code:
    class DrawImageCanvas extends Canvas {
        static Image image;
        
        int count;
        
        public void paint(Graphics g) {
            int width = getWidth();
            int height = getHeight();
    
            // Fill the background using black
            g.setColor(0);
            g.fillRect(0, 0, width, height);
            
            // Load an image from the MIDlet resources
            if (image == null) {
                try {
                    image = Image.createImage("file://C:/Data/Images/200907/200907A0/05072009003.jpg");
                } catch (IOException ex) {
                    g.setColor(0xffffff);
                    g.drawString("Failed to load image!", 0, 0, Graphics.TOP | Graphics.LEFT);
                    return;
                }
            }
            
            switch (count % 3) {
            case 0:
                // Draw the image at the top left of the screen
                g.drawImage(image, 0, 0, Graphics.TOP | Graphics.LEFT);
                break;
    
            case 1:
                // Draw it in the bottom right corner
                g.drawImage(image, width, height, Graphics.BOTTOM | Graphics.RIGHT);
                break;
            case 2:
                // Draw it in the center
                g.drawImage(image, width/2, height/2, Graphics.VCENTER | Graphics.HCENTER);
            }
            count++;
        }
    }

  2. #2
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: load image problem

    The parameter for Image.createImage(String) is a resource name - it has to be the name of something in the JAR. It's not a URL, so you can't use file:// or http:// or anything similar.

    If you want to access a file in the phone's file system, you'll have to use the FileConnection API (passing the file:/// URL to Connector.open()) and obtain an InputStream. Pass the InputStream to Image.createImage(InputStream).

    There will be parts of the phone's file system to which Java apps have no access. Even when you do have access, there will be security prompts, which can be disabled only for a trusted (signed) application.

    Graham.

  3. #3
    Super Contributor
    Join Date
    Feb 2006
    Posts
    689

    Re: load image problem

    Hi,
    Graham is right,You have to use the FileConnection API.

    Thanks
    Soku

Similar Threads

  1. Load Many Image
    By m_afatah in forum Symbian
    Replies: 3
    Last Post: 2009-06-21, 07:56
  2. Load an image to the Canvas by using NetBeans 6.5
    By ochin in forum Mobile Java Media (Graphics & Sounds)
    Replies: 10
    Last Post: 2009-06-19, 11:48
  3. problem in display an image button on canvas(Background image)
    By mramin05 in forum Mobile Java Media (Graphics & Sounds)
    Replies: 3
    Last Post: 2008-11-12, 04:51
  4. How to load a .png image file in device?
    By sungodjava in forum Mobile Java General
    Replies: 3
    Last Post: 2007-08-03, 13:58
  5. Nokia Image Converter
    By davidpurdie in forum General Development Questions
    Replies: 0
    Last Post: 2004-02-18, 15:31

Posting Permissions

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