Search code examples
androidandroid-file

Unable to access file system after Android 9 to 10 change


I have an app that uses files on the storage device. This has been running absolutely fine on Android 9. One of the things it does is browse a directory to get a list of files.

I have just updated to Android 10. All of a sudden, there are (apparently) no files in the directory. However actually all the same files are there according to Total Commander.

The code (folder being a String set to /storage/emulated/0/uk.co.nightshade-arts/maps) is

...
String []
        rawFiles;

try {
    File    f = new File(folder);

    rawFiles = f.list();
}

catch (Exception e) {
        return null;
}

...

rawFiles ends up as null, which is what list() should return if the path is not a folder - but it is a folder.

However, (1) the files have not changed, (2) they are definitely there according to Total Commander and (3) another app with the same storage permissions, target API etc, using the same code to list a (different) directory still works absolutely fine.

Can anyone suggest what might have changed between Android 9 and 10 that might cause this?

As an aside I also notice that Android Studio continually disconnects from the target (Google Pixel 2) so debugging fails now the phone runs 10. That didn't used to happen, either.


Solution

  • You have no access to that directory targetting Android Q indeed.

    That is what has changed.

    No access to getExternalStorageDirectory() anymore.

    Add android:requestLegacyExternalStorage="true" to manifest if you want to continue in the old way until R.