views:

1108

answers:

2

Hi,

I'm writing a MIDlet which needs to write file. I'm using FileConnection from JSR-75 to accomplish this.

The intention is to have this MIDlet runnning on as much devices as possible (all MIDP 2.0 devices with JSR-75 support, ideally).

On several emulators and an HTC Touch Pro2, I can perfectly use the following code to get the root of the filesystem:

Enumeration drives = FileSystemRegistry.listRoots();
String root = (String) drives.nextElement();
String path = "file:///" + root;

However, on a Nokia S60 5th edition emulator, trying to open a FileConnection to this path throws a java.lang.SecurityException. Apparently S60 devices do not allow connections to the root of the filesystem. I realise I can use something like System.getProperty("fileconn.dir.photos"), but that isn't supported on all devices either.

So, my actual question: what is the best approach to get a path to create a FileConnection with, that allows for maximum portability?

Thanks.

Edit: I suppose I could iterate over all the roots in the Enumeration, and check for a writable one, but that's hardly optimal for two reasons. First, there aren't necessarily any writable roots. Second, this could be the phone memory or a memory card, so the storage method wouldn't be consistent across devices, which is rather ugly.

A: 

I will tell you what we do. We have a test app that just finds out the file system root and the SD card root if applicable. We set this as a jad parameter. The code reads it from the Jad file. Since you dont need to recompile the jar for different devices this works out very well, just change the jad parameter for a handset with different file system root.

omermuhammed
That's a lot cleaner, but not really feasible in our case. Our application will need to run on practically all JSR-75 capable devices. We can't check which device allows write access to which root...
benvd
+2  A: 

You are supposed to open read-only connections to roots in order to find out what folder they contain.

As a general rule, when opening a read_write connection to a folder throws a SecurityException, try to open a read-only connection to browse through sub-folders in order to find a writable one.

Specifically on Symbian (and other platforms advanced enough to provide secure data cages to your MIDlets), you can use System.getProperty("fileconn.dir.private"); to find a writable area.

QuickRecipesOnSymbianOS
Yeah, I found out about fileconn.dir.private. If it can't be found, though, I still fall back on trying all the roots. And don't I need to connect in R/W? If I connect read only, I can't figure out if the roots are writable...
benvd
The roots may not be writable but one of their subfolder probably is.
QuickRecipesOnSymbianOS