如何在 Android 框架中写入/读取特定于应用程序的文件? [打开失败:修改 AOSP 时 EACCES(权限被拒绝)]

问题描述 投票:0回答:0

我正在尝试修改其中一个 Android 框架代码,以使其读取/写入用户应用程序的特定于应用程序的文件。

当我检查 logcat 时,它显示以下错误:

java.io.FileNotFoundException: /data/user/0/com.example.APPS/files/FILES: open failed: EACCES (Permission denied)

根据我的搜索,我发现这个问题可以通过使用一些更改访问模式并禁用 SELinux 的 adb 命令来解决:

$ adb root
$ adb shell chmod 777 /data/user/0/com.example.APPS/files
$ adb shell setenforce 0    

但是,我认为这只是一个暂时的解决方案,因为如果设备一旦关闭,它就不起作用了。

所以,我的问题是, Android 框架是否有任何非临时方法来处理特定于应用程序的文件?

具体来说,我通过添加修改了'frameworks/base/core/java/com/android/server/am/BatteryStatsService.java':

FileWriter writer = null;
try {    
    JSONObject oneHist = new JSONObject();
    oneHist.put("a", "b");
    File file = new File(path);
    writer = new FileWriter(file, true);
    writer.write(oneHist.toString() + "\n");
    writer.flush();                
} catch (JSONException | IOException e){
    e.printStackTrace();
} finally {
    try { writer.close(); } catch (Exception e) { }
}
android file android-source
© www.soinside.com 2019 - 2024. All rights reserved.