File path for J2ME FileConnection?
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.
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.
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.
精彩评论