简体   繁体   中英

It can't access the sdcard after setting “android.uid.system” in my application?

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

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.

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");

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM