【问题标题】:AOSP: Write a file on /data directoryAOSP:在 /data 目录上写入文件
【发布时间】:2020-05-07 05:39:51
【问题描述】:

我想在“/data”目录下写一个文件。我已经植根了我的设备并设置了 0。但是,我得到了:

W/System.err: java.io.FileNotFoundException: data/MyDoople.txt (权限被拒绝)

这是我的代码(适用于 sdcard):

String filename= "MyDoople.txt";
try
{
    File f = new File("data/"+File.separator+filename);

    FileOutputStream fOut = new FileOutputStream(f);
    OutputStreamWriter myOutWriter = new OutputStreamWriter(
            fOut);
    myOutWriter.append("Mytest");
    myOutWriter.close();
    fOut.close();
}
catch(Exception e)
{
    e.printStackTrace();
}

我是否必须添加任何政策才能使其发挥作用?

【问题讨论】:

    标签: android android-permissions selinux


    【解决方案1】:

    因为data目录的权限是rwxrwx--x,user system,group system,而你app的运行用户是普通用户,不是系统用户,所以app无法读写/data目录。

    可以参考两种方式:

    1:如果你有平台证书,在AndroidManifest.xml中声明android:sharedUserId="android.uid.system",并使用平台证书对app重新签名。这样,运行你的应用的用户就是系统用户,对/data分区有读写权限。

    2:在app中获取root权限,然后执行相关命令,参考:

        public static boolean RootCommand(String command) {
        Process process = null;
        DataOutputStream os = null;
        try {
            process = Runtime.getRuntime().exec("su");
            os = new DataOutputStream(process.getOutputStream());
            os.writeBytes(command + "\n");
            os.writeBytes("exit\n");
            os.flush();
            process.waitFor();
        } catch (Exception e) {
            Log.d("*** DEBUG ***", "ROOT REE" + e.getMessage());
            return false;
        } finally {
            try {
                if (os != null) {
                    os.close();
                }
                process.destroy();
            } catch (Exception e) {
            }
        }
        Log.d("*** DEBUG ***", "Root SUC ");
        return true;
    }
    

    【讨论】:

    • 嗯...很好的解释...但是可以说我不能扎根(我扎根看看是否可能)。该应用程序是后加载的应用程序(因此不是系统),但我拥有该应用程序的证书。有什么我可以添加的,以便只有这个应用程序可以在 /fwupdate 上写入?
    猜你喜欢
    • 2016-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-10
    • 2011-02-15
    • 2013-01-19
    相关资源
    最近更新 更多