Alif Hasnain
Alif Hasnain

Reputation: 1256

Unable to access storage in Android Q

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.

Upvotes: 3

Views: 5730

Answers (1)

Alif Hasnain
Alif Hasnain

Reputation: 1256

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>

Upvotes: 9

Related Questions