×
Namespaces

Variants
Actions
(Difference between revisions)

Checking memory card in Java ME

From Nokia Developer Wiki
Jump to: navigation, search
olkazmin (Talk | contribs)
(New page: __NOTOC__ __NOEDITSECTION__ {{CodeSnippet |id=  |platform=S60 3rd Edition, FP2 |devices= Nokia N80, Nokia N81 |category=Java ME |subcategory=Hardware |creationdate=October 30, 2008 |k...)
 
seppo_fn (Talk | contribs)
Line 3: Line 3:
 
{{CodeSnippet
 
{{CodeSnippet
 
|id= 
 
|id= 
|platform=S60 3rd Edition, FP2
+
|platform=S60 3rd Edition, S60 3rd Edition, FP1, S60 3rd Edition, FP2
 
|devices= Nokia N80, Nokia N81
 
|devices= Nokia N80, Nokia N81
 
|category=Java ME
 
|category=Java ME

Revision as of 15:56, 8 December 2008


Article Metadata
Tested with
Devices(s): Nokia N80, Nokia N81
CompatibilityArticle
Keywords: java.lang.System.getProperty(), javax.microedition.io.Connector.open(), javax.microedition.io.file.FileConnection.availableSize()
Created: (30 Oct 2008)
Last edited: seppo_fn (08 Dec 2008)

Overview

The code snippet demonstrates how memory card availability can be checked.

App uses System.getProperty method to check API version and memory card drive path.

After the path have been retrieved app establishes a file connection using Connector.open method and gets available amount of space on the memory card by calling FileConnection.availableSize.

Source file: CheckingMemoryCard.java

import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
/**
* MIDP is an example checking memory card availability.
*/

public class CheckingMemoryCard extends MIDlet implements CommandListener {
private Display display;
/**
* For displaying results.
*/

private Form frmMain;
/**
* Calls exitMIDlet method.
*/

private Command cmdExit;
/**
* Starts MC-checking procedure.
*/

private Command cmdCheck;
/**
* Constructor. Constructs the object and initializes displayables.
*/

public CheckingMemoryCard() {
InitializeComponents();
}
/**
* Initializes a ListBox object and adds softkeys.
*/

protected void InitializeComponents() {
display = Display.getDisplay( this );
//initializing device list
frmMain = new Form( "MC Info" );
 
cmdExit = new Command( "Exit", Command.EXIT, 1 );
cmdCheck = new Command( "Check", Command.SCREEN, 1);
 
frmMain.addCommand( cmdExit );
frmMain.addCommand( cmdCheck );
frmMain.setCommandListener( this );
}
 
/**
* From MIDlet.
* Called when MIDlet is started.
* @throws javax.microedition.midlet.MIDletStateChangeException
*/

public void startApp() throws MIDletStateChangeException {
display.setCurrent( frmMain );
}
/**
* From MIDlet.
* Called to signal the MIDlet to enter the Paused state.
*/

public void pauseApp() {
//No implementation required
}
/**
* From MIDlet.
* Called to signal the MIDlet to terminate.
* @param unconditional whether the MIDlet has to be unconditionally
* terminated
* @throws javax.microedition.midlet.MIDletStateChangeException
*/

public void destroyApp(boolean unconditional)
throws MIDletStateChangeException {
exitMIDlet();
}
/**
* From CommandListener.
* Called by the system to indicate that a command has been invoked on a
* particular displayable.
* @param command the command that was invoked
* @param displayable the displayable where the command was invoked
*/

public void commandAction( Command command, Displayable displayable ) {
 
if( command == cmdExit ) {
exitMIDlet();
}
if( command == cmdCheck ) {
//checking API version
if( !checkAPIVersion() )
return;
//getting Localized UI name for the memory card
checkMCDirName();
//checking Root directory of a memory card
if( !checkMCRoot() )
return;
//if Root directory retrieved in the previous step
//then we do a file connection test
checkMCConnection();
}
}
/**
* Method calls stopDiscover method and notifyDestroyed after that.
*/

protected void exitMIDlet() {
notifyDestroyed();
}
/**
* Adds a StringItem to the frmMain.
* @param strPrint string to add to frmMain.
* @see Form#append(java.lang.String)
* @see Form
*/

protected void printToFrm( String strPrint ) {
frmMain.append( strPrint );
}
/**
* Gets JSR-75 API version using MIDP system property and
* check whether it is null or not.
* @return true if version detected and false otherwise.
*/

protected boolean checkAPIVersion() {
String fileconVer =
System.getProperty(
"microedition.io.file.FileConnection.version" );
printToFrm( "Getting JSR-75 API version..." );
if( fileconVer != null ) {
printToFrm( "Version : " + fileconVer );
} else {
printToFrm( "Version NOT found!");
return false;
}
return true;
}
/**
* Gets Localized MC directory name using MIDP system property and
* check whether it is null or not.
* @return true if name detected and false otherwise.
*/

private boolean checkMCDirName() {
String nameMC = System.getProperty( "fileconn.dir.memorycard.name" );
printToFrm( "Getting MC directory name ..." );
if( nameMC != null ) {
printToFrm( "MC directory name : " + nameMC );
} else {
printToFrm( "MC directory name NOT found!");
return false;
}
return true;
}
/**
* Gets MC directory connection URL using MIDP system property and
* check whether it is null or not.
* @return true if URL detected and false otherwise.
*/

private boolean checkMCRoot() {
String rootMC = System.getProperty( "fileconn.dir.memorycard" );
printToFrm( "Getting MC root ..." );
if( rootMC != null ) {
printToFrm( "MC root : " + rootMC );
} else {
printToFrm( "MC root NOT found!");
return false;
}
return true;
}
/**
* Gets MC directory connection URL using MIDP system property and
* check whether it is null or not.
* @return true if URL detected and false otherwise.
*/

private boolean checkMCConnection() {
String rootMC = System.getProperty( "fileconn.dir.memorycard" );
FileConnection fconn = null;
try {
fconn = (FileConnection)Connector.open( rootMC );
printToFrm( "Getting available size ...");
printToFrm( "Size: " + ( fconn.availableSize() / 1024 ) + "KB" );
printToFrm( "Closing file connection...");
fconn.close();
} catch ( Exception e ) {
printToFrm( e.toString() );
return false;
}
return true;
}
}

Postconditions

When Check command is being pressed the folowing steps are executed:

-API availability check. If found it is printed to the form;

-getting Localized UI name for the memory card. If found it is printed to the form;

-checking Root directory of a memory card. If found it is printed to the form;

-then we do a file connection test. Available size printed to the form if connection was successful.


Supplementary material

You can see source file and executable application in attached zip archive. Archive is available for download at Media:Checking_memory_card_in_Java_ME.zip

119 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×