If you are using Internet Explorer, please remove blackberry.com from your compatibility view settings.

Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
grahamb314
Posts: 95
Registered: ‎03-22-2009
My Device: Torch 9800 & Bold 9700

Is media card inserted?

[ Edited ]

Hi,

 

I'm looking at how I can determine if the media card is inserted into the device or not.

 

I have tried this, but the only outputs I get are "system/" and "store/" even when the media card is inserted:

 

Enumeration rootEnum = FileSystemRegistry.listRoots();
while (rootEnum.hasMoreElements()) {
 root = (String) rootEnum.nextElement();
 System.out.println(root); 
} 

 

Basically I'm trying to find out if a file exists on the media card, but doing this, would I assume throw an error when the card is not inserted, so I want to use the above or something similar to find out if the card is inserted, then move onto finding if the file on the card exists.

 

Any help on the above and/or explaining how to find if a file exists too, would be a great help.

 

 

Thanks

 

Graham

Please use plain text.
Developer
KeniF
Posts: 131
Registered: ‎08-13-2008
My Device: Not Specified

Re: Is media card inserted?

-Have you mounted the card? I think if it's mounted you will see it in listroots()

-You may find the System.getProperty method useful - see document for String values to pass in

-To find if a file exists, you could open a FileConnection and use the exists() method.

-Finally, I think it's better to use .toString() than cast it to String. Basically, toString() makes sure it can be a String at compile time but with a cast you may get a ClassCastException. (correct me if I'm wrong).

Please use plain text.
Developer
grahamb314
Posts: 95
Registered: ‎03-22-2009
My Device: Torch 9800 & Bold 9700

Re: Is media card inserted?

[ Edited ]

Thanks for your reply,

 

If I mount the card, there is no output from the code I provided above relating to the card, just store and system, meaning that the listroots doesnt seem to work for the media card.

 

I have looked at System.getProperty but I dont know which key to use. It seems as though there isn't one to check for the availability of a media card.

I tried:

System.out.println(System.getProperty("fileconn.dir.memorycard"));

 

but that outputs:

 

file:///SDCard/

 

regardless of of if the simulator has a media card installed or not

 

Any clues?

 

Please use plain text.
Developer
grahamb314
Posts: 95
Registered: ‎03-22-2009
My Device: Torch 9800 & Bold 9700

Re: Is media card inserted?

There might have been something strange going on but this now works:

 

(For those who need to know how:smileyhappy:

 

 

boolean sd_card_present = false;
String root = null;
Enumeration e = FileSystemRegistry.listRoots();
while (e.hasMoreElements())
{
   root = (String)e.nextElement();

   if(root.equalsIgnoreCase("sdcard/"))
   {
     sd_card_present = true;
   }     

 

As for checking that a file exists, simply

FileConnection fileConnection = null;
boolean db_exists = false;
try {
	fileConnection = (FileConnection)Connector.open("file:///SDCard/"+ file_path);
} 
catch (IOException e1) {
	System.out.println("Need to check what this exception is for");
	e1.printStackTrace();
}

/* If file exists set variable to true */
if(fileConnection.exists())
{                      
	db_exists = true;
}
else{
// Do Something else
}

 

If someone could point me to what the IO exception is for, that would be great

 

 

Please use plain text.
Developer
KeniF
Posts: 131
Registered: ‎08-13-2008
My Device: Not Specified

Re: Is media card inserted?

The reason I mentioned System.getProperty() is you should never assume the root name of the media card remains the same, or your software could suddenly break in the future :smileyhappy:

Please use plain text.
Developer
grahamb314
Posts: 95
Registered: ‎03-22-2009
My Device: Torch 9800 & Bold 9700

Re: Is media card inserted?

Very good point!

 

Thanks

Please use plain text.