Android 10: JAVA: How to list all files in a directory


With Android < 10, we can list all files in a directory with this code:

                File directory = new File(path);
                File[] files = directory.listFiles();

But with Android 10, you receive a blank list. How to fix this problem?

Simple, you can use below code, and only add this line of code to your manifest in the application tag:

android: requestLegacyExternalStorage= "true"

Example:

    <application
        android:requestLegacyExternalStorage="true"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:networkSecurityConfig="@xml/network_security_config"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.tutorialspots"
        android:usesCleartextTraffic="true">

Leave a Reply