Blackberry Development 5: FileConnection APIs

Refer to http://developers.sun.com/mobility/apis/articles/fileconnection/ for the API.

FileConnectionTest.java

package com.henry416.fileconnection;

import net.rim.device.api.ui.UiApplication;

public class FileConnectionTest extends UiApplication {

    public FileConnectionApplication() {
        FileConnectionScreen screen = new FileConnectionScreen();
        pushScreen(screen);
     }

    public static void main(String[] args) {
        FileConnectionTest app = new FileConnectionTest();
        app.enterEventDispatcher();
     }

}

FileConnectionScreen.java

package com.henry416.fileconnection;

import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.component.ObjectListField;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.util.StringProvider;

public class FileConnectionScreen extends MainScreen {

    private ObjectListField fileList;
    private String currentPath = "file:///";

     public FileConnectionScreen() {
         setTitle("FileConnection");

        fileList = new ObjectListField();

        fileList.set(new String[] {"store/", "SDCard/"});

        add(fileList);
     }

    protected void makeMenu(Menu menu, int instance) {
        super.makeMenu(menu, instance);
        menu.add(new MenuItem(new StringProvider("Select"), 10, 10) {
            public void run() {
                loadFile();
            }
        });
     }

    private void loadFile() {
        currentPath += fileList.get(fileList, fileList.getSelectedIndex());
        try {
            FileConnection fileConnection = (FileConnection)Connector.open(currentPath);
            if (fileConnection.isDirectory()) {
                Enumeration directoryEnumerator = fileConnection.list();
                Vector contentVector = new Vector();
                while(directoryEnumerator.hasMoreElements()) {

                    contentVector.addElement(directoryEnumerator.nextElement());
                }
                String[] directoryContents = new String[contentVector.size()];
                contentVector.copyInto(directoryContents);

                fileList.set(directoryContents);
            }

        } catch (IOException ex) {

        }
    }
}

For more samples are here.

About henry416
I am a computer technology explorer and an university student based on Toronto. If you have any question, please feel free to discuss and comment here

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s