【发布时间】:2017-12-01 13:43:45
【问题描述】:
我正在编写一个可以在 Android 4.4 上完美运行但无法在 Android 7 上运行的应用。 两台设备都已root。
我的应用程序的目的是从其他应用程序的数据目录(我们称之为 com.game.orig)中获取文件,将它们复制到我的应用程序目录(我们称之为 com.game.cheat)并在那里读取修改它们,然后再将它们写回原始目录。
所以使用“su”(抛出在那里找到的函数ExecuteAsRootBase.java) 我将文件从 com.game.orig 复制到 com.game.cheat 目录,如下所示:
PackageManager m = context.getPackageManager();
String appDir = getPackageDirectory("com.game.orig",m);// /data/user/0/com.game.orig/files/
String localDir = getPackageDirectory(context.getPackageName(),m);// /data/user/0/com.game.cheat/files/
if (ExecuteAsRootBase.canRunRootCommands()) {
ExecuteAsRootBase rootBase = new ExecuteAsRootBase();
Sting fileName="savedgame.dat";
int uid=context.getApplicationInfo().uid;//get the uid (owner) of my app.
//Create localDir (files subdirectory) if not exists
File directory = new File(localDir);
if (!directory.exists()) {
directory.mkdirs();
//adjust file permission (not sure it's realy needed)
rootBase.executecmd("chmod 777 "+ localDir);
rootBase.executecmd("chown " + uid + "." + uid + " " + localDir);
}
//copy file from appDir to localdir using 'su'
rootBase.execute("cp "+ appDir +fileName + " " + localDir)){
//adjust file permission
rootBase.execute("chmod 777 "+ localDir +fileName);
rootBase.execute("chown " + uid + "." + uid + " " + localDir + fileName);
}
到此结束,一切正常:
我的文件目录具有权限:drwxrwxrwx,归 u0_a115 组 u0_a115 所有。 (匹配 /data/data/com.game.cheat 的所有者/组) 以及我复制的具有相同所有者/组和权限的文件:-rwxrwxrwx
现在尝试打开复制的文件进行读取时:
InputStream input = context.openFileInput( fileName );
openFileInput 抛出异常:
java.io.FileNotFoundException: /data/user/0/com.game.cheat/files/savedgame.dat (Permission denied)
而且这只发生在搭载 Android 7.0 API24 的手机上。
有人对我的方法有什么问题有一些提示,我错过了最新 API 的新内容吗?
谢谢。
【问题讨论】:
-
是的,从 Android 6.0 及更高版本开始,读取和写入文件被视为危险权限,您需要为此征得用户的明确许可。要了解有关此结帐的更多信息,请点击此链接developer.android.com/guide/topics/permissions/…
标签: android file-permissions android-context android-7.0-nougat rooted-device