繁体   English   中英

无法访问 Android Q 中的存储

[英]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/时, subFilesnull 我不知道这是否与新的 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.

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