×
Namespaces

Variants
Actions
(Difference between revisions)

Checking memory card in Java ME

From Nokia Developer Wiki
Jump to: navigation, search
mtilli (Talk | contribs)
m (Mtilli -)
hamishwillee (Talk | contribs)
m (Hamishwillee - Add Abstract. Tidy wiki text)
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
[[Category:Java ME]][[Category:Code Examples]][[Category:Code Snippet]]
+
[[Category:Java ME]][[Category:Code Examples]][[Category:Base/System]][[Category:Files/Data]][[Category:Symbian]][[Category:Series 40]][[Category:Java Runtime 2.3 for Symbian]][[Category:Nokia Belle]][[Category:Series 40 Developer Platform 1.1]][[Category:Series 40 Developer Platform 2.0]][[Category:Series 40 3rd Edition FP1]][[Category:Series 40 5th Edition (initial release)]][[Category:Series 40 6th Edition (initial release)]][[Category:Series 40 6th Edition FP1]][[Category:Series 40 5th Edition FP1]][[Category:Series 40 3rd Edition FP2]][[Category:S60 3rd Edition (initial release)]][[Category:S60 5th Edition]]
 +
{{Abstract|This code snippet demonstrates how to check memory card availability.}}
 +
 
 
{{ArticleMetaData <!-- v1.2 -->
 
{{ArticleMetaData <!-- v1.2 -->
|sourcecode= [[Media:Checking memory card in Java ME.zip]]  
+
|sourcecode= [[Media:Checking memory card in Java ME.zip]]
 
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
 
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
|devices= Nokia 6131, Nokia N80, Nokia N81, Nokia 701
+
|devices= Nokia 6131, Nokia N80, Nokia N81, Nokia 701, Nokia Asha 305
|sdk= <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Nokia Qt SDK 1.1]) -->
+
|sdk= [http://www.developer.nokia.com/Develop/Java/Tools/ Nokia SDK 1.1 for Java],  [http://www.developer.nokia.com/Develop/Java/Tools/ Nokia SDK 2.0 for Java]
|platform= Series 40 3rd Edition, FP1, S60 3rd Edition, S60 3rd Edition, FP1, S60 3rd Edition, FP2
+
|platform= Series 40, Symbian
 
|devicecompatability= <!-- Compatible devices (e.g.: All* (must have GPS) ) -->
 
|devicecompatability= <!-- Compatible devices (e.g.: All* (must have GPS) ) -->
 
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
 
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
Line 22: Line 24:
 
|author= [[User:Olkazmin]]
 
|author= [[User:Olkazmin]]
 
<!-- The following are not in current metadata -->
 
<!-- The following are not in current metadata -->
|subcategory= Hardware
 
 
|id= CS001200
 
|id= CS001200
 
}}
 
}}
  
 
==Overview==
 
==Overview==
This code snippet demonstrates how to check memory card availability.
 
  
 
The application uses the {{Icode|System.getProperty}} method to check the API version and memory card drive path.
 
The application uses the {{Icode|System.getProperty}} method to check the API version and memory card drive path.

Revision as of 09:40, 5 October 2012

This code snippet demonstrates how to check memory card availability.

Article Metadata
Code ExampleTested with
Devices(s): Nokia 6131, Nokia N80, Nokia N81, Nokia 701, Nokia Asha 305
CompatibilityArticle
Keywords: java.lang.System.getProperty(), javax.microedition.io.Connector.open(), javax.microedition.io.file.FileConnection.availableSize()
Created: olkazmin (27 Nov 2008)
Last edited: hamishwillee (05 Oct 2012)

Contents

Overview

The application uses the System.getProperty method to check the API version and memory card drive path.

After the path has been retrieved, the application establishes a file connection using the Connector.open method and gets the amount of space available 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 the Check command is pressed, the following steps are performed:

  • API availability check. If available, 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.
  • File connection test. The available size is printed to the form if the connection

was successful.


Supplementary material

The source file and executable application are available for download at Media:Checking memory card in Java ME.zip.

173 page views in the last 30 days.
×