簡體   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