×

Discussion Board

Results 1 to 13 of 13

Thread: File connection

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    34

    File connection

    I want to save an image in my moible..
    For that i want to create a png file and save the image code in that..
    but i am not able to create a file properly.
    i used following code
    http://wiki.forum.nokia.com/index.ph...ample_-_JSR_75

    its getting exit after i press the save button....
    but the file gets saved properly..
    and app is not able to overwrite previous file (if present)..

    I have not signed the midlet!!
    Is it required for file connection????

  2. #2
    Registered User
    Join Date
    Oct 2009
    Location
    Noida
    Posts
    941

    Re: File connection

    Quote Originally Posted by dgreatkumar View Post
    I want to save an image in my moible..
    For that i want to create a png file and save the image code in that..
    but i am not able to create a file properly.
    i used following code
    http://wiki.forum.nokia.com/index.ph...ample_-_JSR_75

    its getting exit after i press the save button....
    but the file gets saved properly..
    and app is not able to overwrite previous file (if present)..

    I have not signed the midlet!!
    Is it required for file connection????
    Yes, using FCA API's may require siging on real devices - test your application on SUN WTK emulator.
    Thanks,
    Ekta

  3. #3
    Nokia Developer Champion
    Join Date
    Mar 2005
    Location
    Paris
    Posts
    814

    Re: File connection

    no it may not. at least, not on any device that i know of.
    moreover, if the file is written, it does indicate that the problem is somewhere else. can you past the snippet that performs the save ?

  4. #4
    Registered User
    Join Date
    Sep 2010
    Posts
    34

    Re: File connection

    I tried this code:

    Code:
    import java.io.InputStream;
    import java.io.OutputStream;
    import javax.microedition.io.Connector;
    import javax.microedition.io.file.FileConnection;
    import javax.microedition.lcdui.Alert;
    import javax.microedition.lcdui.Command;
    import javax.microedition.lcdui.CommandListener;
    import javax.microedition.lcdui.Display;
    import javax.microedition.lcdui.Displayable;
    import javax.microedition.lcdui.TextBox;
    import javax.microedition.lcdui.TextField;
    import javax.microedition.midlet.MIDlet;
    import javax.microedition.midlet.MIDletStateChangeException;
     
    public class MIDletfinal extends MIDlet implements CommandListener {
     
    	Command save, exit;
     
    	TextBox text;
     
    	Display display;
     
    	public MIDletfinal() {
     
    		text = new TextBox("", "", 400, TextField.ANY);
    		save = new Command("Save", Command.SCREEN, 1);
    		exit = new Command("Exit", Command.EXIT, 1);
    		text.addCommand(save);
    		text.addCommand(exit);
    		text.setCommandListener(this);
     
    	}
     
    	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
    		// TODO Auto-generated method stub
     
    	}
     
    	protected void pauseApp() {
    		// TODO Auto-generated method stub
     
    	}
     
    	protected void startApp() throws MIDletStateChangeException {
    		display = Display.getDisplay(this);
    		display.setCurrent(text);
     
    	}
     
    	public void commandAction(Command arg0, Displayable arg1) {
     
    		if (arg0 == save) {
    			saveFile();
    			// Alert used for notify the user that the file had already been
    			// saved
    			Alert alert = new Alert("File saved.");
    			alert.setTimeout(Alert.FOREVER);
    			display.setCurrent(alert, this.text);
     
    		}
    		if (arg0 == exit)
    			;
    		{
    			try {
    				destroyApp(true);
    				this.notifyDestroyed();
    			} catch (MIDletStateChangeException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
     
    		}
     
    	}
     
    	private void saveFile() {
     
    		try {
    			// Creating a connection.
    			FileConnection c = (FileConnection) Connector.open(System
    					.getProperty("fileconn.dir.photos")
    					+ "bruno/", Connector.READ_WRITE);
    			// Checking if the directoy exists or not. If it doesn't exist we
    			// create it.
    			if (c.exists()) {
    				System.out.println("existe");
     
    			} else {
    				System.out.println("nao existe");
    				c.mkdir();
    				c = (FileConnection) Connector.open(System
    						.getProperty("fileconn.dir.photos")
    						+ "bruno/thiago.txt", Connector.READ_WRITE);
    				// create the file
    				c.create();
    				// create an OutputStream
    				OutputStream out = c.openOutputStream();
    				// Get the user text
    				String userText = text.getString();
    				// write out the user's text into the file
    				out.write(userText.getBytes());
    				out.flush();
    				out.close();
    				// Never forget to close a connection or you can face problems.
    				// Pay attention here! If you close the connection before and
    				// later try to
    				// write something it will throw an exception.
    				c.close();
     
    				// Openning the file again and
    				// show what was saved in console.
    				c = (FileConnection) Connector.open(System
    						.getProperty("fileconn.dir.photos")
    						+ "bruno/thiago.txt", Connector.READ_WRITE);
    				//
    				InputStream in = c.openInputStream();
    				byte[] b = new byte[50];
    				in.read(b);
    				System.out.println(new String(b, 0, b.length));
     
    			}
    		} catch (Exception e) {
     
    		}
     
    	}
     
    }

  5. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    34

    Re: File connection

    @njzk2 :
    its available on this link-
    http://wiki.forum.nokia.com/index.ph...ample_-_JSR_75

  6. #6
    Nokia Developer Champion
    Join Date
    Mar 2005
    Location
    Paris
    Posts
    814

    Re: File connection

    Code:
    		if (arg0 == exit)
    			;
    		{
    this does not work as you would like.
    the condition is evaluated, the ';' is executed conditionnally, and then the code between brackets is always executed.
    remove the semi-colon and it will work.

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

    Re: File connection

    I have to say,

    Code:
    if (arg0 == exit)
    	;
    {
    That ";" looks like a mistake. It should read:

    Code:
    if (arg0 == exit) {
    Also:

    Code:
    c.create();
    will throw an IOException if the file already exists. Try:

    Code:
    if (!c.exists()) {
        c.create();
    }
    And: you'll want to move the closing "}" for the "else" clause up to after the c.mkdir() call. Otherwise, this code does nothing if the directory already exists.

    Graham.

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

    Re: File connection

    Hmmm... I think it ought to look like:

    Code:
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.io.IOException;
    import javax.microedition.io.Connector;
    import javax.microedition.io.file.FileConnection;
    import javax.microedition.lcdui.Alert;
    import javax.microedition.lcdui.Command;
    import javax.microedition.lcdui.CommandListener;
    import javax.microedition.lcdui.Display;
    import javax.microedition.lcdui.Displayable;
    import javax.microedition.lcdui.TextBox;
    import javax.microedition.lcdui.TextField;
    import javax.microedition.midlet.MIDlet;
    
    public class FileWriter extends MIDlet implements CommandListener {
        private Command save, exit;
        private TextBox text;
        private Display display;
         
        protected void destroyApp(boolean unconditional) {
            // TODO Auto-generated method stub
        }
         
        protected void pauseApp() {
            // TODO Auto-generated method stub
        }
         
        protected void startApp() {
            if (display == null) {
                display = Display.getDisplay(this);
                text = new TextBox("", "", 400, TextField.ANY);
                save = new Command("Save", Command.SCREEN, 1);
                exit = new Command("Exit", Command.EXIT, 1);
                text.addCommand(save);
                text.addCommand(exit);
                text.setCommandListener(this);
            }
            display.setCurrent(text);
        }
         
        public void commandAction(Command command, Displayable displayable) {
            if (command == save) {
                // Alert used for notify the user that the file had already been
                // saved
                Alert alert;
                try {
                    saveFile();
                    alert = new Alert("File saved.");
                } catch (Exception e) {
                    alert = new Alert(e.toString());
                }
                alert.setTimeout(Alert.FOREVER);
                display.setCurrent(alert);
            }
            if (command == exit) {
                destroyApp(true);
                notifyDestroyed();
            }
        }
         
        private void saveFile() throws IOException {
            String folder = System.getProperty("fileconn.dir.photos") + "bruno/";
             
            // Creating a connection.
            FileConnection c = (FileConnection) Connector.open(folder, Connector.READ_WRITE);
            try {
                // Checking if the directoy exists or not. If it doesn't exist we
                // create it.
                if (c.exists()) {
                    System.out.println("existe");
                } else {
                    System.out.println("nao existe");
                    c.mkdir();
                }
            } finally {
                c.close();
            }
            
            c = (FileConnection) Connector.open(folder + "thiago.txt", Connector.READ_WRITE);
            try {
                if (!c.exists()) {
                    // create the file
                    c.create();
                }
            
                // create an OutputStream
                OutputStream out = c.openOutputStream();
                try {
                    // Get the user text
                    String userText = text.getString();
                    // write out the user's text into the file
                    out.write(userText.getBytes());
                } finally {
                    out.close();
                }
            } finally {
                // Never forget to close a connection or you can face problems.
                // Pay attention here! If you close the connection before and
                // later try to
                // write something it will throw an exception.
                c.close();
            }
        }
    }
    If anyone fancies testing it and letting me know if it's better or worse (reply back here), I'll update the wiki.

    Graham.

  9. #9
    Registered User
    Join Date
    Sep 2010
    Posts
    34

    Re: File connection

    @grahamhughes:
    hey,your code is working fine...
    thanks for this all.

    but how to sign a midlet???
    Is it necessary for file connection?????

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

    Re: File connection

    Thanks for letting me know. I'll update the wiki.

    No, you don't need to sign to use the File Connection API.

    Signing might help reduce the number of annoying security prompts. Or it might not. Signing is a whole world of pain and confusion. And it costs you money. Avoid like the plague.

    If you want to learn about signing, start here.

    Graham.

  11. #11
    Registered User
    Join Date
    Sep 2010
    Posts
    34

    Re: File connection

    @grahamhughes:
    can we remove the security question without signing the MIDlet????

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

    Re: File connection

    You might be able to change from "ask every time" to "ask first time" in the settings for the application. What settings are available, and where they are, depends on the device.

    Signing might enable the "always allow" option (or it might not), but it will not select it by default.

    Graham.

  13. #13
    Registered User
    Join Date
    Sep 2010
    Posts
    34

    Re: File connection

    @grahamhughes: ok fine...

Similar Threads

  1. http connection & File Connection where The problem?
    By hussam9_96 in forum Mobile Java Networking & Messaging & Security
    Replies: 1
    Last Post: 2011-01-10, 09:16
  2. How to encounter end of File using File Connection API's
    By pvumesh in forum Mobile Java General
    Replies: 8
    Last Post: 2010-01-27, 11:04
  3. can we cast file connection to file
    By ansh.chauhan in forum Mobile Java General
    Replies: 2
    Last Post: 2008-11-27, 05:06
  4. File System(File Connection API)
    By amalshah73 in forum Mobile Java General
    Replies: 1
    Last Post: 2007-08-26, 02:41
  5. File connection.
    By balagopalks in forum Mobile Java General
    Replies: 4
    Last Post: 2004-12-15, 05:06

Posting Permissions

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