繁体   English   中英

在我的应用程序中设置“android.uid.system”后无法访问sdcard?

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

这是一个奇怪的问题。 如果我没有设置android.uid.system ,我的应用程序可以成功访问 SD 卡。 但是在将android.uid.system设置为它之后,我的应用程序无法访问 sdcard。 此时发生异常: 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后,我的应用程序属于系统进程,应该有更多的权力来访问 sdcard。 这是我的想法。 以下是我的代码:

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。 非常感谢。

请阅读: link1

这个链接2

您可以在 android 源代码中看到:frameworks\base\core\java\android\os\Environment.java,它有一个 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());
    }
}

这个 function 将被称为getExternalStorageDirectory() ,因此如果您不破解 aosp,具有系统 uid 的应用程序将无法访问 sdcard。

使用Environment.getExternalStorageDirectory().getAbsolutePath()获取路径,而不是"/mnt/sdcard"

使用: File share = new File(Environment.getExternalStorageDirectory().getAbsolutePath(),"test.txt");

这不起作用: File share = new File("/mnt/sdcard", "test.txt");

android.uid.system 使您的应用程序绑定系统,但系统也观察到sdcard,然后如果您的应用程序删除它,它可以访问sdcard。 它似乎

删除 xml 文件中的这一行:android:sharedUserId="android.uid.system"ystem

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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