FileNotFound 打开文件时 EACCESS Android Marshmallow

FileNotFound EACCESS when opening file Android Marshmallow

在我的产品的自动化测试期间,我将文件推送到应用程序的私有目录和 chmod 文件,以便应用程序可以访问它。在 APIs < Marshmallow 上,这很好,应用程序在访问文件时从未遇到过问题。

现在,我在尝试打开某些文件时收到以下日志:

W/System.err(7669): Caused by: 
java.io.FileNotFoundException: /data/user/0/foo/foo.txt: open failed: EACCES (Permission denied) 09-28 18:20:56.724: 
W/System.err(7669):     at libcore.io.IoBridge.open(IoBridge.java:452) 09-28 18:20:56.724: 
W/System.err(7669):     at java.io.FileInputStream.<init>(FileInputStream.java:76) 09-28 18:20:56.724: 
W/System.err(7669):     at android.app.ContextImpl.openFileInput(ContextImpl.java:384) 09-28 18:20:56.724: 
W/System.err(7669):     at android.content.ContextWrapper.openFileInput(ContextWrapper.java:177) 09-28 18:20:56.724: 
W/System.err(7669): Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied) 09-28 18:20:56.724: 
W/System.err(7669):     at libcore.io.Posix.open(Native Method) 09-28 18:20:56.724: 
W/System.err(7669):     at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186) 09-28 18:20:56.724: 
W/System.err(7669):     at libcore.io.IoBridge.open(IoBridge.java:438) 09-28 18:20:56.716: 
W/pool-5-thread-1(8279): type=1400 audit(0.0:57): avc: denied { search } for name="files" dev="mtdblock1" ino=7314 scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:object_r:app_data_file:s0 tclass=dir permissive=0

我该如何解决?

Android Marshmallow 采用了其底层 OS、SELinux(安全增强 Linux)的扩展文件属性,因此推送文件时会出现问题和偏好通过 shell 而不是通过对 FS 具有权限的应用程序在应用程序之间随意选择。

首先,确保您 chmod 文件正确。然后,确保该文件也被正确拥有。您可以通过执行 ls -la file 检查普通文件的所有者和组的外观,然后通过 chgrp user.group file

将有问题的文件的组更改为您从上一个命令中找到的组
$ ls -la  
drwxrwxrwx u0_a69   u0_a69            2015-09-28 18:20 existing_file
drwxrwxrwx root   root            2015-09-28 18:20 file    
$ chown u0_a69.u0_a69 file

如果这些都不起作用,请通过 ls -Z file 检查扩展文件属性并阅读 https://fedoraproject.org/wiki/Security_context?rd=SELinux/SecurityContext 以了解您正在查看的内容,然后使用 chcon some:extended:security:attributes file 更改您的文件权限。

$ ls -Z 
 drwxrwx--x u0_a69   u0_a69 u:object_r:app_data_file:s0:c512,c768 existing_file    
 drwxrwx--x u0_a69   u0_a69 u: file
$ chcon u:object_r:app_data_file:s0:c512,c768 file

如果仍然无效,/system/bin 中有一个名为 setenforce 的二进制文件,它禁用 Marshmallow 底层 SELinux.

的扩展文件系统安全性

从 root shell 调用 setenforce permissive,您的应用程序将能够访问它需要的文件。这可能会掩盖错误,因此请谨慎使用。

$ setenforce permissive

在Android清单文件中尝试放入

<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="19"/>
    <uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 

并且在运行时你可以在你的设置->应用->权限中检查授予的权限或直接授予访问所有权限,看看它是否有效...

public boolean isStoragePermissionGranted() {
            if (Build.VERSION.SDK_INT >= 23) {
                if(checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)== PackageManager.PERMISSION_GRANTED) {
                    return true;
                } else {
                    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
                    return false;
                }
            } else {
                return true;
            }
        }