This is a strange problem. My Application can access the sdcard successfully if I don't set android.uid.system
to it. But after setting android.uid.system
to it, my application can't access the sdcard. At this time, the exception take place:07-13 09:11:24.999开发者_高级运维: INFO/System.out(9986): create file happen exception--->java.io.IOException: Permission denied
. I check I write the right permission in the right place:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />.
Because use forceStopPackage
in my application, I need to add android.uid.system
to the manifest. And I have written LOCAL_CERTIFICATE := platform
in the make file. Who can explain this strange problem. After setting android.uid.system
, my application is belong the system process which should have more power to access the sdcard. This is my idea. The following are my code:
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() );
}
}
And My application run at the emulator and his target version is 2.3. Thank you very much.
Please read this: link1
and this link2
Use Environment.getExternalStorageDirectory().getAbsolutePath()
to get the path, NOT "/mnt/sdcard"
Use: File share = new File(Environment.getExternalStorageDirectory().getAbsolutePath(),"test.txt");
This won't work: File share = new File("/mnt/sdcard", "test.txt");
You can see in android source code: frameworks\base\core\java\android\os\Environment.java, it have a function:
private static void throwIfSystem() {
if (Process.myUid() == Process.SYSTEM_UID) {
Log.wtf(TAG, "Static storage paths aren't available from AID_SYSTEM", new Throwable());
}
}
This function would be called getExternalStorageDirectory()
, hence app with system uid can't access sdcard if you don't hack aosp.
android.uid.system make your app bind system, but sdcard observered by system also, then if your app delete this, it can access the sdcard. it seems
delete this line in your xml file :android:sharedUserId="android.uid.system"ystem
精彩评论