×

Discussion Board

Results 1 to 6 of 6
  1. #1
    Registered User
    Join Date
    Feb 2007
    Posts
    5

    Can't show immutable image in Form

    Im using a Nokia 6680 phone and I need to load an immutable image from the memory card and put in on a Form on the display. I can get the connection with the image, because it asks for reading and writing permissions, but I don't seem to get the Image on the display. The code I use is the following:

    Code:
    import java.io.*;
    import java.util.*;
    import java.io.OutputStream;
    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;
    import javax.microedition.media.*;
    import javax.microedition.media.control.*;
    import javax.microedition.io.*;
    import javax.microedition.io.file.*;
    import java.io.IOException;
    
    public class Picture extends MIDlet implements CommandListener {
        
        InputStream is = null;
        Image img = null;
        byte[] slide;
        Image photo = null;
        private Form form, mainform = new Form("Picture");;
        String str = null;
        static final Command okCommand = new Command("OK",Command.ITEM,0);
        Display display;
        
        /** Creates a new instance of Picture */
        public Picture() {
            display = Display.getDisplay(this);
            initialize();
        }
    
        /** This method initializes UI of the application.                        
         */
        private void initialize() {                               
            mainform.append("Test Picture");
            mainform.addCommand(okCommand);
            mainform.setCommandListener(this);
            
            set_main();       
        }                     
        
        public void commandAction(Command c, Displayable displayable){                    
           String label = c.getLabel();
           
           if(label.equals("OK")){
               
               try {
                   photo=get_photo();
                   form.append(new ImageItem("", photo, ImageItem.LAYOUT_TOP, null));
                   show_form(form);
               } catch (IOException ex) {
                   ex.printStackTrace();
               }
                        
            }
        }     
           
           void set_main(){
               display.setCurrent(mainform);
           }
           
           public void show_form(Form f){
               display.setCurrent(f);
           }
           
        /**
         * This method should return an instance of the display.
         */
        public Display getDisplay() {                         
            return Display.getDisplay(this);
        }                        
        
        public Image get_photo() throws IOException{
            FileConnection fc = (FileConnection)Connector.open("file:///E:/Images/04042006.jpg");
            is = fc.openInputStream();
            int len = (int) fc.fileSize();
            slide = new byte[len];
            img = Image.createImage(slide, 0, len);
            return img;
        }
        
        /**
         * This method should exit the midlet.
         */
        public void exitMIDlet() {                         
            getDisplay().setCurrent(null);
            destroyApp(true);
            notifyDestroyed();
        }                        
        
        public void startApp() {
        }
        
        public void pauseApp() {
        }
        
        public void destroyApp(boolean unconditional) {
        }
        
    }

    Does anyone know how to solve this problem with a Form? Many thanks in advance,

    Tom

  2. #2
    Registered User
    Join Date
    May 2007
    Posts
    4

    Re: Can't show immutable image in Form

    Maybe I'm missing something, but in the following code, I don't see how you are reading in the image:

    FileConnection fc = (FileConnection)Connector.open("file:///E:/Images/04042006.jpg");
    is = fc.openInputStream();
    int len = (int) fc.fileSize();
    slide = new byte[len];
    img = Image.createImage(slide, 0, len);
    return img;

    You get the input stream, then get the file size, then initialize a byte array to the proper size, then create an image from the null-filled byte array.

    Could this be the problem. If I'm missing something blatantly obvious, please forgive - I'm working on very little sleep.

    Cheers

  3. #3
    Registered User
    Join Date
    Feb 2007
    Posts
    5

    Re: Can't show immutable image in Form

    If i'm right then
    Code:
    img = Image.createImage(slide, 0, len);
    reads in the image. Slide is then the array that contains the data about the image-file, 0 is the offset (so it starts from the first element in the array) and len is the length of the array, so the filesize. Normally this should create an image... but it seems like it doesn't do that...

    Can anyone help with this please?

  4. #4
    Registered User
    Join Date
    Feb 2007
    Posts
    5

    Re: Can't show immutable image in Form

    Meanwhile I found a small flaw in the code, but it still doesn't work properly like this:
    Code:
    public Image get_photo() throws IOException{
            FileConnection fc = (FileConnection)Connector.open("file:///E:/Images/04042006.jpg");
    if(!fc.exists()) {
            fc.create();
            is = fc.openInputStream();
            int len = (int) fc.fileSize();
            slide = new byte[len];
            img = Image.createImage(slide, 0, len);
            }
            fc.close();
    
            return img;
        }
    I forgot to create the fileconnection, quite stupid... But, even with creating the fileconnection it still doesn't work...

  5. #5
    Registered User
    Join Date
    May 2007
    Posts
    4

    Re: Can't show immutable image in Form

    I still don't see where you're reading the image into memory. You opened a file stream but never read the bytes into the array.

    The line

    img=Image.createImage(slide, 0, len);

    doesn't actually read from the input stream, it just creates an image of that size from the byte array - which is still empty. You'll need to fill the byte array first.

    Add this line before creating the image:

    is.read(slide, 0, len);

    Cheers

  6. #6
    Registered User
    Join Date
    Feb 2007
    Posts
    5

    Re: Can't show immutable image in Form

    After a while searching we found an example of Nokia, and after looking through that we found the solution for displaying the pictures... so it works now... finally!!!
    Thanks though for the useful tips!

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. show image in form
    By D|3-H4rD in forum Mobile Java Media (Graphics & Sounds)
    Replies: 5
    Last Post: 2006-06-08, 23:28
  4. HELP: Mutable Image to Immutable Image?
    By rj_cybersilver in forum Mobile Java Media (Graphics & Sounds)
    Replies: 1
    Last Post: 2005-03-26, 09: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
  •  
×