Monday, July 11, 2011

JavaMe Tips: FileConnection API useful methods

Hello all. Today I'm going to show you two very useful methods when working with the FileConnection API for JavaMe. This methods along with others not described in this post, can help you a lot:

  
  //inside a class...

  /**
   * Checks whether the device has support for the 
   * FileConnection API
   * @return true if and only if the device supports the API
   * false otherwise
   */
  private boolean checkFileSupport() {
    if (System.getProperty(
       "microedition.io.file.FileConnection.version") != null)
    {
      return true;
    }
    return false;
  }

The last method tells you whether the device has support for the FileConnection API. Pretty handy when the application is developed for a wide range of mobile devices.
Next, we'll see a method that returns all the possible drives where the files can be stored in the mobile device.

  
   //inside a class...

  /**
   * Asks for the list of drivers where files can be stored
   * @return Vector of Strings representing the name of
   * the drives where files can be stored
   */
  private Vector getRoots() {
    Vector vRoots = new Vector();

    if (checkFileSupport()) {
      Enumeration drivers = FileSystemRegistry.listRoots();
      while (drivers.hasMoreElements()) {
        vRoots.addElement((String) drivers.nextElement());
      }
    }
    return vRoots;
  }

Imagine your application takes snapshots, and you want to ask the user where to store them... with the help of the last method  and a List, you can show to the user all the possible "hard drives" where he/she can save their pictures.

see ya soon!

References:

Getting Started with the FileConnection APIs. December 2004. Oracle [online].
Available on Internet: http://developers.sun.com/mobility/apis/articles/fileconnection/
[accessed on February 26 2011].

Working with the Java ME FileConnection API on Physical Devices. March 2007. Java.net[online].
Available on Internet: http://today.java.net/article/2007/03/27/working-java-me-fileconnection-api-physical-devices
[accessed on February 26 2011].