J2ME FileConnection 的文件路径?
我正在编写一个需要写入文件的MIDlet。我使用 JSR-75 中的 FileConnection 来完成此任务。
目的是让这个 MIDlet 在尽可能多的设备上运行(最好是所有支持 JSR-75 的 MIDP 2.0 设备)。
在多个模拟器和 HTC Touch Pro2 上,我可以完美地使用以下代码来获取文件系统的根目录:
Enumeration drives = FileSystemRegistry.listRoots();
String root = (String) drives.nextElement();
String path = "file:///" + root;
但是,在诺基亚 S60 第五版模拟器上,尝试打开此路径的 FileConnection 会引发 java.lang.SecurityException。显然,S60 设备不允许连接到文件系统的根目录。我意识到我可以使用类似 System.getProperty("fileconn.dir.photos") 的东西,但并非所有设备都支持。
所以,我的实际问题:获取创建 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该打开与根目录的只读连接,以便找出它们包含的文件夹。
作为一般规则,当打开到文件夹的 read_write 连接抛出 SecurityException 时,请尝试打开只读连接来浏览子文件夹,以便找到可写的文件夹。
特别是在 Symbian(以及其他足以为您的 MIDlet 提供安全数据笼的先进平台)上,您可以使用 System.getProperty("fileconn.dir.private"); 来查找可写区域。
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.我会告诉你我们做什么。我们有一个测试应用程序,它只查找文件系统根目录和 SD 卡根目录(如果适用)。我们将其设置为 jad 参数。该代码从 Jad 文件中读取它。由于您不需要为不同的设备重新编译 jar,因此效果非常好,只需更改具有不同文件系统根的手机的 jad 参数即可。
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.