解决java.lang.SecurityException: Access denied
NOKIA的一些目录不允许创建文件,所以会抛出java.lang.SecurityException: Access denied,只需做个提示就行了,或引导用户进入其子目录创建文件,问题便能解决,示例代码如下:
private void createFile(String fileName) {
String fileExt = "";
{
int idx = fileName.lastIndexOf('.');
if (idx > 0) {
fileExt = fileName.substring(idx, fileName.length());
fileName = fileName.substring(0, idx);
}
}
try {
currDir = (FileConnection) Connector.open("file:///" + currDirName
+ fileName + fileExt, Connector.WRITE);
currDir.create();
}catch (SecurityException e) {
Alert alert = new Alert("Error!", "Can not access/delete file "
+ fileName + " in directory " + currDirName
+ "\nException: " + e.getMessage(), null, AlertType.ERROR);
alert.setTimeout(Alert.FOREVER);
FileMIDlet.display.setCurrent(alert);
}catch (IOException e) {
e.printStackTrace();
try {
currDir = (FileConnection) Connector.open("file:///"
+ currDirName + fileName + "(" + genId() + ")"
+ fileExt, Connector.WRITE);
currDir.create();
} catch (IOException e1) {
// TODO Auto-generated catch block
currDir = null;
}
}
}
谢谢你的回答,我把Connector.READ_WRITE改为Connector.WRITE,异常依旧。我看一下你的参考文献吧。 :D
会不会是Connector.READ_WRITE这里问题,Connector.WRITE,http://www.forum.nokia.com/info/sw.nokia.com/id/82644083-2f4b-4775-a292-c02d6bf5be57/Introduction_To_The_FileConnection_API_v1_1.zip.html这里有个例子,也是写到fileconn.dir.photos的,可能看看!
你好,非常感谢你的回答。这几天一直找时间按你说的来测试,但还是有许多问题,再次向你请教,麻烦了。 我的目的是想把拍到的相片存到存储卡中,不想存到RMS中,因为存储卡比较大(2G)。我的代码如下: public void save(){ String name=imageName.getString();
String initDir = System.getProperty("fileconn.dir.photos"); System.out.println(initDir); // String initDir = "file:///e:/Nokia/Images/"; try{ System.out.println( initDir + name); FileConnection fc = (FileConnection) Connector.open(initDir + name, Connector.READ_WRITE); fc.create();
} 我分别模拟机Prototype_4_0_S40_128x128_MIDP_Emulator和Prototype_4_0_S60_MIDP_Emulator上测试。(security domain都为real_life,security domain为trusted_3rd_party我也测过) 情况如下: 1,Prototype_4_0_S40_128x128_MIDP_Emulator 当String initDir = System.getProperty("fileconn.dir.photos");时, (即initDir = file:///c:/Nokia/Images/),报异常java.lang.SecurityException: Application not authorized to access the restricted API; 当String initDir = "file:///e:/Nokia/Images/";时,也报异常java.lang.SecurityException: Application not authorized to access the restricted API。 2,Prototype_4_0_S60_MIDP_Emulator 当String initDir = System.getProperty("fileconn.dir.photos");时, (即initDir = file:///c:/Nokia/Images/),报异常java.lang.SecurityException: Application not authorized to access the restricted API; 当String initDir = "file:///e:/Nokia/Images/";时,也报异常java.lang.SecurityException: Application not authorized to access the restricted API。
然后我在真机nokia6300上测试,情况如下: String initDir = System.getProperty("fileconn.dir.photos");和 String initDir = "file:///e:/Nokia/image/";时,都出现提示:应用软件被设为不允许访问。
请问我想把相片存到内存卡中,我应该怎么写?谢谢。
一般出现这种异常有两种情况,一、写文件时文件名重复,二、文件目录不能写入,第一种可用上面的CODE的解决,第二种你可提示用户些目录不给写入,请到别一个目录,NOKIA的机子一般都是在C盘私有目录时会不给写入,再进入下一层可以写入。而写入卡时不会出现这个异常!
你好,请问在模拟机上运行不会抛出java.lang.SecurityException: Access denied异常,而在真机上测试会抛出java.lang.SecurityException: Access denied异常,这是什么原因呢?是否是因为权限问题,我把权限设为最大maximum。谢谢。