How to browse file structure of mobile device with JME
Example of simple way to browse files on mobile phone
Last month when I finished the ElectronicPersonalOrganiser (1, 2, and 3) I was thinking how cool it would be have option to add image to contact. For this, midlet should allow user to browser mobile phone directories and find the file. Question was how to do it?
After little bit of research I remembered that Jonathan Knudsen and Sing Li mentioned something about optional packages and file connection in their book Beginning J2ME: From Novice to Professional. And there was it! Chapter 9 - Persistent Storage II: File Connection and PIM API, pg 117 - 130. Provided examples are overkill for what I had in mind, but they are good start.
File Connection together with Personal Information Management (PIM) is part of JSR 75 - PDA Optional Packages for the J2ME™ Platform (online API from Forum Nokia Library). Despite the fact that most of today's mobile phones have this archive/library there are some odd cases without them, so it is safer to check before any file snooping.
private void runDetection() { if (System.getProperty( "microedition.io.file.FileConnection.version") != null) { changeScreen(CoreUI.FILE_BROWSER); } else { StringBuffer deviceData = new StringBuffer(getAppProperty("MIDlet-Name")) .append("\n").append(getAppProperty("MIDlet-Vendor")) .append("\nFileConnection API is not available"); Alert alert = new Alert("Missing API", deviceData.toString(), null, AlertType.INFO); alert.setTimeout(Alert.FOREVER); Display.getDisplay(this).setCurrent(alert); } }
The method will check with System class if there is any version of FileConnection available. This can result either in error message that this package is missing (Image 1)and on alert dismiss exit midlet, or package is there and user is redirected to GUI screen.![]() |
![]() |
![]() |
| Image 1 - Package missing | Image 2 - Root view from emulator | Image 3 - Tree view of folders |
Now once we are past the package availability test we can start poking around. For start finding out available roots (Image 2, please notice there are 3 items listed. However root 1 can be only seen with application running from emulator.)
public Vector getRoot() { Vector dirList = new Vector(); Enumeration e = FileSystemRegistry.listRoots(); while (e.hasMoreElements()) { dirList.addElement(new FileSysObject((String) e.nextElement(), FileSysObject.ROOT)); } return dirList; }
With list of roots available on the device we can start querying system about it's tree structure as FileConnection fconn = (FileConnection)Connector.open("file:///" + currLocation);. On midlet start currLocation does equal to empty string currLocation = ""; and to move down the tree I can add c:/ for example currLocation = currLocation + "c:/";. Whole method for listing items bellow root level likes like this:
public Vector getList(String currLocation) { Enumeration e = null; FileConnection fileCon = null; try { fileCon = (FileConnection) Connector.open("file:///" + currLocation); e = fileCon.list(); if (fileCon != null) { fileCon.close(); } } catch (IOException ioe) { ioe.printStackTrace(); } /** * Create new Vector object from getObjectTypes() return with allocation * of folder or file for retrieved items. */ Vector dirList = getObjectTypes(e, currLocation); return dirList; }
getList() also has call for getObjectTypes() method that allocates object type (folder or file) to each retrieved item so I can show these folder icons as seen on Image 3.
Moving up and down the tree structure is controlled by traverseDirectory() method
private void traverseDirectory(String fileName){ if(fileName.equals(GO_UP)){//Want to go up file tree int i = currLocation.lastIndexOf(FOLDER_END, currLocation.length() - 2); if(i != -1){//Character found currLocation = currLocation.substring(0, i +1); } else{//Character not found as we are at first level of one of the roots currLocation = ""; } } else{//Want to go down the file tree currLocation = currLocation + fileName; } setList(); }
Java ME String class was striped of such useful method as astIndexOf(String str) little hack is in place int i = currLocation.lastIndexOf(FOLDER_END, currLocation.length() - 2); (basically saying ignore last back slash "/" character and look for second last)Source of this midlet is available for download here.
Few quick enhancements coming to mind:
- Sorting by alphabetic order, first folders then files as we know it from our computes.
- Showing relative position in tree structure through Screen title option.
- Provide file preview in case of images, sounds or simple text files




