My android java app cant see any .xml files in external storage

I`m making an app to test the Opencv haarcascades and it seems like my app does not see the .xml files. My permission include:

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

My code for collecting the file names and path is:

public ArrayList<String []> getLongAndShortFileHandles(String root_dir) {

        ArrayList<String[]> ret = new ArrayList<>();

        String path = Environment.getExternalStorageDirectory().toString()+"/"+root_dir;

        File directory = new File(path);
        File[] files = directory.listFiles();
        Log.e(TAG, "getLongAndShortFileHandles: Has dir "+path+", with "+((files==null) ? "null" : files.length) + " number of files");
        for (int i = 0; i < Objects.requireNonNull(files).length; i++)
            Log.e(TAG, "getLongAndShortFileHandles: HAS FILE "+files[i].getName());
            String [] temp = new String [] {files[i].getAbsolutePath(),files[i].getName()};

        return ret;

The debug shows that no files are present when I call this function to populate my RecycleView subclass like so:

        this.cascade_selector = (RecyclerView) findViewById(;
        this.cascade_selector.setAdapter(new FileAdaptah(getLongAndShortFileHandles("cascade_app/cascades"),this, CASCADE));

        this.xml_ano_selector = (RecyclerView) findViewById(;
        this.xml_ano_selector.setAdapter(new FileAdaptah(getLongAndShortFileHandles("cascade_app/annotations"),this, ANNOTATION_XML));

The result in the app does show the output for second call but not for the first one. If the first one is replaced with any other folder like DCIM, or Music it shows the files.

However if I add .xml file into any directory it will not be shown.


I was playing around with different directories and files and determined that the problem goes further then just .xml it seem to be all the ascii encoded text file formats I could think of. All binaries like .jpg, .wav, .mp4, .bin and etc. are found in when put in same directory.

EDIT (2): So I`ve established that if I rename any of the text file extensions into any binary one (e.g. 50.xml -> 50.jpg they are detected by the script.

This prompted me to try different filename filters, but they didn’t yield any success.

I`ve also tried to use Directory Stream instead but to no avail:

    @RequiresApi(api = Build.VERSION_CODES.O)
    public ArrayList<String[]> getFiles(String suffix, String root_dir){
        ArrayList<String[]> ret = new ArrayList<String[]>();
        try {
            root_dir = Environment.getExternalStorageDirectory().toString() + "/" + root_dir;
            Path dirName = Paths.get(root_dir);
            DirectoryStream<Path> paths = Files.newDirectoryStream(dirName, "*.pdf");

            paths.forEach(path -> {
                Log.e(TAG, "getFiles: HAS FILE : "+path.toString());
                if (path.endsWith(suffix)) {
                    String sPath = path.toString();
                    String[] splitted = sPath.split(File.separator);
                    ret.add(new String[]{sPath, splitted[splitted.length - 1]});
        } catch (IOException exception) {

        return ret;

I`ve started to suspect it might be a bug, so I provide my environment information: My android studio is version 4.1.2 and my target SDK is 30, while my minimum accepted is 19. My test device is a physical Galaxy A40 with android 11.

EDIT (3): So I’ve noticed that the second snippet for getFiles has leftover glob expression from the point I’ve copied the original snippet. Out of interest I’ve modified and removed it but in either case nothing changed.

EDIT (4): So I’ve tried to access file directly with the following code:

        this.test = (Button) findViewById(;
        this.test.setOnClickListener(v -> {
            String path = Environment.getExternalStorageDirectory().toString()+"/cascade_app/cascades/50.xml";
            Intent intent = new Intent(v.getContext(),CheckCascade.class);
            File debugFile = new File(path);
            Toast.makeText(this, "File "+path+" does "+((debugFile.exists())? "exist":"not exist"), Toast.LENGTH_SHORT).show();


and it seem to detect it no problem.


So after some time I was helped by a kind person who suggested to check if my app needs this permission. This solved the problem! I hope this can help people who will have same issue.