Unable to access storage in Android Q

风格不统一 提交于 2020-03-03 05:47:04

问题


Although having read and write permissions I am unable to access sdcard in Android 10. This was my approach to list all directory.

        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);
            }
        }

If I use /mnt/ instead of /mnt/sdcard/ I can see sdcard directory is listed in there but when I use /mnt/sdcard/ , subFiles is null. I don't know if this is something regarding new android 10 version or it is a problem with emulator. I was using android studio's default emulator for debuging.


回答1:


This is a temporary solution. From Android 11 Scoped Storage have to be used.

The problem was requestLegacyExternalStorage was not set to true in the manifest file. Because of scoped access on external storage by default it is set to false in Android 10 or higher. Adding this line in the AndroidManifest.xml solved my problem.

<manifest ... >
  <!-- This attribute is "false" by default on apps targeting
       Android 10 or higher. -->
  <application 
       android:requestLegacyExternalStorage="true" ........ >
    ......
  </application>
</manifest>


来源:https://stackoverflow.com/questions/58213996/unable-to-access-storage-in-android-q

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!