×

Discussion Board

Results 1 to 7 of 7
  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    16

    Question persistent file storage on phone memory

    Hello,

    I have the following problem: I need to store a string permanently on the phone no matter what changes I make to the midlet suite.

    So far I tried to store it in a recordstore, but the recordstores are bound to the midlet suite. So if I change the midlet suite or reinstall it, the stored data is gone.

    Then I tried the fileconnection API. I had several problems accessing folders, for example am I not allowed to create a folder inside the data/ folder of my 6110, so I tried a different approach: With System.getProperty("fileconn.dir.private"); I tried to use the private folder of the midlet suite, but the same problem as with the recordstores applies here though.

    Finally I tried to use the folder the call to System.getProperty("fileconn.dir.photos"); returned and it worked. But I don't like this solution for obvious reasons.

    So, how is this done properly? Is there a general data folder I can use?

  2. #2
    Nokia Developer Expert
    Join Date
    Aug 2007
    Posts
    1,595

    Re: persistent file storage on phone memory

    Hello molp,

    when using FC API, the file path depends on whether your are using a Series 40 or an S60 device.

    Try using this MIDlet for finding a path for your file:
    import java.util.*;
    import java.io.*;
    import javax.microedition.io.*;
    import javax.microedition.io.file.*;
    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;

    public class FileMidlet extends MIDlet implements CommandListener
    {
    private String currDirName;

    private Command view = new Command("View", Command.ITEM, 1);
    private Command back = new Command("Back", Command.BACK, 2);
    private Command exit = new Command("Exit", Command.EXIT, 3);
    Form naytto= new Form("Form");

    private final static String UP_DIRECTORY = "..";
    private final static String MEGA_ROOT = "/";
    private final static String SEP_STR = "/";
    private final static char SEP = '/';

    public FileMidlet()
    {
    currDirName = MEGA_ROOT;
    }

    public void startApp()
    {
    boolean isAPIAvailable = false;
    if (System.getProperty(
    "microedition.io.file.FileConnection.version") != null)
    {
    isAPIAvailable = true;
    try
    {
    showCurrDir();
    }
    catch (SecurityException e)
    {}
    catch (Exception e) {}
    }
    else
    {
    StringBuffer splashText = new StringBuffer(
    getAppProperty("MIDlet-Name")).append("\n").append(
    getAppProperty("MIDlet-Vendor")).
    append(isAPIAvailable?"":"\nFileConnection API not available");
    Alert splashScreen = new Alert(null,splashText.toString(),
    null,AlertType.INFO);
    splashScreen.setTimeout(3000);
    Display.getDisplay(this).setCurrent(splashScreen);
    }

    }

    public void pauseApp() {}

    public void destroyApp(boolean cond)
    {
    notifyDestroyed();
    }

    public void commandAction(Command c, Displayable d)
    {
    if (c == view)
    {
    List curr = (List)d;
    final String currFile = curr.getString(curr.getSelectedIndex());
    new Thread(new Runnable()
    {
    public void run()
    {
    if (currFile.endsWith(SEP_STR) ||
    currFile.equals(UP_DIRECTORY))
    {
    traverseDirectory(currFile);
    } else
    {
    showFile(currFile);
    }
    }
    }).start();
    }
    else if (c == back)
    {
    showCurrDir();
    }
    else if (c == exit)
    {
    destroyApp(false);
    }
    }

    void showCurrDir()
    {
    Enumeration e;
    FileConnection currDir = null;
    List browser;
    try
    {
    if (MEGA_ROOT.equals(currDirName))
    {
    e = FileSystemRegistry.listRoots();
    browser = new List(currDirName, List.IMPLICIT);
    }
    else
    {
    currDir = (FileConnection)Connector.open(
    "file://localhost/" + currDirName);
    e = currDir.list();
    browser = new List(currDirName, List.IMPLICIT);
    browser.append(UP_DIRECTORY, null);
    }
    while (e.hasMoreElements())
    {
    String fileName = (String)e.nextElement();
    if (fileName.charAt(fileName.length()-1) == SEP)
    {
    browser.append(fileName, null);
    }
    else
    {
    browser.append(fileName, null);
    }
    }
    browser.setSelectCommand(view);
    browser.addCommand(exit);
    browser.setCommandListener(this);
    if (currDir != null)
    {
    currDir.close();
    }
    Display.getDisplay(this).setCurrent(browser);
    }
    catch (IOException ioe)
    {}
    }

    void traverseDirectory(String fileName)
    {
    if (currDirName.equals(MEGA_ROOT))
    {
    if (fileName.equals(UP_DIRECTORY))
    {
    // can not go up from MEGA_ROOT
    return;
    }
    currDirName = fileName;
    }
    else if (fileName.equals(UP_DIRECTORY))
    {
    // Go up one directory
    // TODO use setFileConnection when implemented
    int i = currDirName.lastIndexOf(SEP, currDirName.length()-2);
    if (i != -1)
    {
    currDirName = currDirName.substring(0, i+1);
    }
    else
    {
    currDirName = MEGA_ROOT;
    }
    }
    else
    {
    currDirName = currDirName + fileName;
    }
    showCurrDir();
    }

    void showFile(String fileName)
    {
    if(!fileName.endsWith(".jpg")){
    try
    {
    FileConnection fc = (FileConnection)
    Connector.open("file://localhost/" + currDirName + fileName);
    if (!fc.exists())
    {
    throw new IOException("File does not exists");
    }
    InputStream fis = fc.openInputStream();
    byte[] b = new byte[1024];
    int length = fis.read(b, 0, 1024);
    fis.close();
    fc.close();

    TextBox tb = new TextBox("View File: " + fileName, null, 1024,
    TextField.ANY | TextField.UNEDITABLE);

    tb.addCommand(back);
    tb.addCommand(exit);
    tb.setCommandListener(this);

    if (length > 0)
    {
    tb.setString(new String(b, 0, length));
    }
    Display.getDisplay(this).setCurrent(tb);
    }
    catch (Exception e) {
    System.out.println(e.getMessage());
    }
    }

    else if(fileName.endsWith(".jpg")){

    try{
    FileConnection fc = (FileConnection)
    Connector.open("file:////" + currDirName +fileName);
    if (fc.exists()) {
    InputStream fis = fc.openInputStream();
    long overallSize = fc.fileSize();

    int length = 0;
    byte[] imageData = new byte[0];
    while (length < overallSize) {
    byte[] data = new byte[1024];
    int readAmount = fis.read(data, 0, 1024);
    byte[] newImageData = new byte[imageData.length + 1024];
    System.arraycopy(imageData, 0, newImageData, 0, length);
    System.arraycopy(data, 0, newImageData, length, readAmount);
    imageData = newImageData;
    length += readAmount;
    }
    fis.close();
    fc.close();
    Image currentImage = Image.createImage(imageData, 0, length);

    naytto.append(currentImage);
    Display.getDisplay(this).setCurrent(naytto);
    }
    else
    {
    // display.setCurrent(requestList);
    }

    } catch (IOException io){System.out.println(io.getMessage());

    }
    }



    }
    }
    Let us know if you need more info on this matter.

    Regards,
    r2j7
    Last edited by r2j7; 2008-08-06 at 12:08.
    [URL="http://library.forum.nokia.com/java"][B] >>> Java Developer's Library <<<[/B][/URL]
    [URL="https://www.developer.nokia.com/Resources/Support/Technical_support.xhtml"] [B]>>> Technical Support for Java ME development <<<[/B][/URL]
    [URL="https://publish.ovi.com/info/"][B]>>> Nokia Publish: reach millions of Nokia users worldwide through Nokia Store <<<[/B][/URL]

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    16

    Re: persistent file storage on phone memory

    Thanks for your reply r2j7,

    I tried your MIDlet and found out, that there is a directory on the phone memory that is called "C:/data/". I tried to create a folder inside this folder and I tried to create a file, but both failed due to an "access denied" error. I also can't create a folder or file under the root diretory.

    Then I found the directory "Telefonspeicher/Privat/" which I can write into and create directories and files. But if the MIDlet is deleted and reinstalled, or overwritten by a newer version the data is lost.

    I am looking for a general directory that I can use for whatever I want to and which is not linked to the MIDlet in any way, e.g. also not erased if the MIDlet changes.

    Regards,

  4. #4
    Nokia Developer Expert
    Join Date
    Aug 2007
    Posts
    1,595

    Re: persistent file storage on phone memory

    Hello molp,

    when using the MIDlet, if you browse further from C:/data there should be folders like Images, Sounds and Videos. Include one of these folders within your file path.

    More on this from this resource: http://www.forum.nokia.com/info/sw.n..._0_en.zip.html

    "..there is an extra layer of restrictions with respect to file access. Depending on the security domain the MIDlet has been assigned during installation, it will have access to a subset of the file system. This is designed to protect the user data and prevent damage to the operating system. In particular, MIDlets
    located in the trusted third-party and untrusted domains have access only to a set of designated public directories including those for images, videos, public files, and a private directory assigned to each MIDlet for its own usage. This is one reason why using virtual roots is recommended since access to the Images/ root may be allowed but doing traversal from c:/ to c:/data/Images/ may not be allowed, because c: could not be accessible by a MIDlet."
    Regards,
    r2j7
    [URL="http://library.forum.nokia.com/java"][B] >>> Java Developer's Library <<<[/B][/URL]
    [URL="https://www.developer.nokia.com/Resources/Support/Technical_support.xhtml"] [B]>>> Technical Support for Java ME development <<<[/B][/URL]
    [URL="https://publish.ovi.com/info/"][B]>>> Nokia Publish: reach millions of Nokia users worldwide through Nokia Store <<<[/B][/URL]

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    16

    Thumbs up Re: persistent file storage on phone memory

    Thanks r2j7,

    I now store my data in the Images folder. Luckily the system property "fileconn.dir.photos" is known on all handsets I tried so far.

  6. #6
    Nokia Developer Champion
    Join Date
    Nov 2007
    Location
    Rome, Italy
    Posts
    2,405

    Re: persistent file storage on phone memory

    Hi molp,

    you could also try using the FileConnection setHidden() method so that (on devices supporting this feature) you will have your data files not publicly visible by users, even if they're placed on default photos folder.

    Hope it helps,
    Pit

  7. #7
    Registered User
    Join Date
    Mar 2008
    Posts
    16

    Thumbs up Re: persistent file storage on phone memory

    Quote Originally Posted by jappit View Post
    Hi molp,

    you could also try using the FileConnection setHidden() method so that (on devices supporting this feature) you will have your data files not publicly visible by users, even if they're placed on default photos folder.
    Thanks jappit,

    that did work. Feels much better to have the file hidden. One more step to prevent the user from accidentally deleting or modifying the data.

Similar Threads

  1. Problem with pyobfuscate
    By JOM in forum Symbian
    Replies: 3
    Last Post: 2008-06-20, 22:47
  2. Replies: 1
    Last Post: 2008-01-12, 11:42
  3. Replies: 1
    Last Post: 2007-12-18, 07:58
  4. How to browse file in phone memory by using Flashlite + Java.
    By pao_karb in forum Mobile Java General
    Replies: 0
    Last Post: 2007-12-18, 07:19
  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
  •