Wednesday, March 4, 2009

Code : Determine if a microSD card has been inserted


The BlackBerry® Pearl™ 8100 smartphone was the first BlackBerry device to support microSD cards. Along with microSD card support, the BlackBerry Pearl 8100 smartphone was the first BlackBerry device to support JSR 75, the FileConnection API. In addition to providing the ability to store data to the internal memory and microSD card, this API also allows an application developer to determine if a microSD card has been inserted into the BlackBerry Pearl 8100 smartphone.

String root = null;
Enumeration e = FileSystemRegistry.listRoots();
while (e.hasMoreElements()) {
root = (String) e.nextElement();
if( root.equalsIgnoreCase(“sdcard/”) ) {
//device has a microSD inserted
} else if( root.equalsIgnoreCase(“store/”) ) {
//internal memory identifier
}
}

The above code will allow a developer to determine if a microSD card is currently inserted, while the code below will notify the developer if this state ever changes (card removed/card inserted). Note that the listener must be added to FileSystemRegistry via the addFileSystemListener() method.

public class MySDListener implements FileSystemListener {
public void rootChanged(int state, String rootName) {
if( state == ROOT_ADDED ) {
if( rootName.equalsIgnoreCase(“sdcard/”) ) {
//microSD card inserted
}
} else if( state == ROOT_REMOVED ) {
//perform the same check as above
}
}
}

As of the last updated date of this article, an API did not exist to determine if the BlackBerry device could support microSD regardless of whether or not a card was inserted. However, the BlackBerry device model number can be used to determine microSD card support since the list of BlackBerry devices that support this feature is finite.

String modelNum = DeviceInfo.getDeviceName();
if(modelNum.startsWith("8") && !modelNum.startsWith("87")) {
//microSD card supported
}

No comments:

Post a Comment

Place your comments here...