繁体   English   中英

Android Marshmallow:新文件(...)授予权限被拒绝

[英]Android Marshmallow: new File (…) gives permission denied

编辑以反映进度。

如果已经回答了这个问题,我深表歉意。

当前正在编写一个应用程序,其中的内容(在一个小的zip文件中)是从外部位置下载的,并存储在/ data / data / package_name_removed / files /中,以便稍后阅读。

我目前在该目录“ Test.zip”中有一个zip文件。

一个try-catch循环,包含:

//where filename is Test.zip


Log.d("Target file is", sourceContext.getFilesDir() +"/"+ fileName);
File file = new File(sourceContext.getFilesDir() +"/"+ fileName);
ZipFile loadedFile = new ZipFile(file);  

//where sourceContext is passed into this object from the base Activity class

在棉花糖中似乎不起作用:

D/Target file is: /data/user/0/package_name_removed/files/Test.zip
W/package_name_removed: type=1400 audit(0.0:11699): avc: denied { open } for name="Test.zip" dev="mmcblk0p29" ino=57426 scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:object_r:app_data_file:s0 tclass=file permissive=0

这是IOException getMessage的Log.d; 我认为这是从新的File()语句生成的:

D/Log_title_omitted: /data/user/0/package_name_removed/files/Test.zip: open failed: EACCES (Permission denied)

我在棒棒糖上工作过; 也在调试它以用于棒棒糖之前,直到我最近升级到棉花糖为止。 我认为在我解决这个问题之前我不能安全地继续,因为这意味着我的应用程序将不支持6.0。

我已经设法在同一个应用程序中毫无问题地将文本文件写入该目录,所以我不明白为什么尝试从该目录打开zip文件不起作用。

编辑:我也试图将有问题的zip文件解压缩到其自己的目录,但无济于事:

//where file is now a json file "Test.json"
//located in .getFilesDir()/Test/
String loadedName = fileName.substring(0, fileName.length() - 5); //I need to reuse the string later.".json" is 5 char.
Log.d("Target is", sourceContext.getFilesDir() +"/"+ loadedName +"/" + fileName);
File file = new File(sourceContext.getFilesDir() + "/"+ loadedStoryName +"/" + fileName);

并得到:

D/DOH: /data/user/0/package_name_removed/files/Test/Test.json: open failed: EACCES (Permission denied)
W/package_name_removed: type=1400 audit(0.0:22152): avc: denied { search } for name="Test" dev="mmcblk0p29" ino=57184 scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:object_r:app_data_file:s0 tclass=dir permissive=0

我了解Marshmallow现在确实在执行SELinux,因此我必须为此编写代码-但我还无法弄清楚如何在代码中获得适当的权限。 正确地,根据文档,我在任何地方都不需要READ_EXTERNAL,也无需请求对内部存储中应用程序自己文件目录的许可。

有什么帮助吗?

注意:包名省略,不重要。

从用户CommonsWare:

“ Rooting不会禁用SELinux”-否,但这并不意味着您通过根shell创建的文件会以某种方式获得相同的权限位/ ACL / etc。 作为>通过应用程序代码创建的文件。 adb shell运行方式可能会以这种方式表现更好。

是的,确实是因为我自己复制了文件,而不是让应用程序来完成。 全面拥有r权限不足以满足SELinux,该应用必须通过“创建”该文件来“拥有”该文件。

我尚未在外部写入目录上对此进行测试,但是对于内部应用程序自己的目录而言,这是正确的。

暂无
暂无

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

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