简体   繁体   English

Android 4.4(API19)和Android 7(API24)之间的文件权限是否有变化?

[英]Any change on file permissions between Android 4.4 (API19) and Android 7 (API24)?

I'm writing an app that works perfectly on an Android 4.4 but failed to work on an Android 7. The 2 devices are rooted. 我正在编写一个可以在Android 4.4上完美运行但无法在Android 7上运行的应用程序。这2个设备已植根。

The aim of my app is to take files from the data directory of an other application (let's call it com.game.orig), copy them to my application directory (let's call it com.game.cheat) and there read them, modify them before writing them back to the original directory. 我的应用程序的目的是从另一个应用程序的数据目录(称为com.game.orig)中获取文件,将它们复制到我的应用程序目录(称为com.game.cheat)中,然后在其中读取,修改在将它们写回到原始目录之前。

So using "su" (throw the function found there ExecuteAsRootBase.java ) i copy files from com.game.orig to com.game.cheat directory like this: 因此,使用“ 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);        
}

Ending here, everything works fine: 至此,一切正常:

i have my files directory with perms: drwxrwxrwx and owned by u0_a115, group u0_a115. 我有带烫发的文件目录:drwxrwxrwx,属于u0_a115,组u0_a115。 (that matches the owner/group of /data/data/com.game.cheat) and my copied files with same owner/group and perms: -rwxrwxrwx (与/data/data/com.game.cheat的所有者/组匹配)以及具有相同所有者/组和权限的复制文件:-rwxrwxrwx

now when trying to open the copied file to read it: 现在,当尝试打开复制的文件以读取它时:

InputStream input = context.openFileInput( fileName );

the openFileInput throw an exception: openFileInput抛出异常:

java.io.FileNotFoundException: /data/user/0/com.game.cheat/files/savedgame.dat (Permission denied)

And this occcurs only on the phone with Android 7.0 API24. 而且这仅在具有Android 7.0 API24的手机上发生。

Anyone have some hints about what is wrong in my approch, di i missed something new with latest API? 任何人都暗示我的做法有什么问题,是否错过了最新API的新功能?

thanks. 谢谢。

Until API 23 the permissions in manifest were enough for you to access the external storage of a device along side other kind of permissions. 在API 23之前,清单中的权限足以使您与其他类型的权限一起访问设备的外部存储。 From API 23 some permissions must be granted by the user in runtime, such as the permission to write in external storage. 通过API 23,用户必须在运行时授予某些权限,例如写入外部存储的权限。

Here is an example how you could do it: 这是一个示例,您可以如何做:

private boolean checkWriteExternalPermission() {

    String permission_read = Manifest.permission.READ_EXTERNAL_STORAGE;
    String permission_write = Manifest.permission.WRITE_EXTERNAL_STORAGE;
    int res = this.checkCallingOrSelfPermission(permission_read);
    int res2 = this.checkCallingOrSelfPermission(permission_write);
    return (res == PackageManager.PERMISSION_GRANTED && res2 == PackageManager.PERMISSION_GRANTED);
}

Now you only need to apply this to your method such as: 现在,您只需要将此方法应用于您的方法,例如:

if(checkWriteExternalPermission()){
    //do your logic with file writing/reading
} else{
    //ask for user permissions
    ActivityCompat.requestPermissions(this, PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE);
}

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

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