无法访问Android Q中的存储 [英] Unable to access storage in Android Q

查看:404
本文介绍了无法访问Android Q中的存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管具有读写权限,但我无法在Android 10中访问sdcard。这是我列出所有目录的方法。

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

如果我使用 / mnt / 而不是 / mnt / sdcard / 我可以看到其中列出了 sdcard 目录但是当我使用 / mnt / sdcard / 时, subFiles null 。我不知道这与新的android 10版本有关,还是模拟器有问题。我正在使用android studio的默认仿真器进行调试。

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.

推荐答案


这是一个临时解决方案。从Android 11开始必须使用范围存储。

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

问题是 requestLegacyExternalStorage 在清单文件中未设置为 true 。由于默认情况下对外部存储的作用域访问,在Android 10或更高版本中,它设置为 false 。在 AndroidManifest.xml 中添加这一行解决了我的问题。

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>

这篇关于无法访问Android Q中的存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆