这是一个奇怪的问题。如果我没有将android.uid.system
设置为SD卡,我的应用程序可以成功访问SD卡。但是在将android.uid.system
设置为它之后,我的应用程序不能访问SD卡。此时,异常发生了:07-13 09:11:24.999: INFO/System.out(9986): create file happen exception--->java.io.IOException: Permission denied
。我检查我在正确的位置写了正确的权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />.
因为在我的应用程序中使用forceStopPackage
,所以我需要将android.uid.system
添加到清单中。并且我已经在make文件中编写了LOCAL_CERTIFICATE := platform
。谁能解释这个奇怪的问题。在设置android.uid.system
后,我的应用程序属于系统进程,它应该有更多的权力访问SD卡。这是我的主意。以下是我的代码:
public void setPackage(String dir){
System.out.println( "setPackage dir=="+dir );
File share=new File("/mnt/sdcard","test.txt");
if(!share.exists()){
try{
share.createNewFile();
}catch(Exception e){
System.out.println( "creat file happen exception--->" +e.toString() );
}
}
try{
if(share!=null){
System.out.println( "create file is not null" );
FileOutputStream fops=new FileOutputStream(share);
fops.write(dir.getBytes());
fops.flush();
fops.close();
}
}catch(Exception e){
System.out.println( "write Exception-->" +e.toString() );
}
}
我的应用程序在仿真器上运行,他的目标版本是2.3。非常感谢。
发布于 2011-07-13 20:49:47
使用Environment.getExternalStorageDirectory().getAbsolutePath()
而不是"/mnt/sdcard"
获取路径
发布于 2011-07-13 20:50:26
用法:File share = new File(Environment.getExternalStorageDirectory().getAbsolutePath(),"test.txt");
这不会起作用:File share = new File("/mnt/sdcard", "test.txt");
https://stackoverflow.com/questions/6679011
复制相似问题