[英]Unable to access storage in Android Q
盡管具有讀寫權限,但我無法訪問 Android 10 中的 sdcard。這是我列出所有目錄的方法。
File root = new File("/mnt/sdcard/");
File[] subFiles = root.listFiles();
for(File file : subFiles) {
if(file.isDirectory()) {
String str = text.getText().toString() + "\n";
str+=file.getAbsolutePath();
text.setText(str);
}
}
如果我使用/mnt/
而不是/mnt/sdcard/
我可以看到其中列出了sdcard
目錄但是當我使用/mnt/sdcard/
時, subFiles
是null
。 我不知道這是否與新的 android 10 版本有關,或者是模擬器的問題。 我正在使用 android studio 的默認模擬器進行調試。
這是一個臨時解決方案。 從 Android 開始,必須使用 11 范圍存儲。
問題是清單文件中的requestLegacyExternalStorage
未設置為true
。 由於默認情況下對外部存儲的范圍訪問,它在 Android 10 或更高版本中設置為false
。 在AndroidManifest.xml
中添加這一行解決了我的問題。
<manifest ... >
<!-- This attribute is "false" by default on apps targeting
Android 10 or higher. -->
<application
android:requestLegacyExternalStorage="true" ........ >
......
</application>
</manifest>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.