'Missing permission for Android document folder

I'm trying to access files in the android document folder. The code below works as long as the current installation of the app has created the files. After reinstalling the app or adding documents any other way the app can't access the files anymore. The file list shows up empty. Upon creating a new file, the newly created file is listed and accessible. I suspect my app is not allowed to use files in the document folder, if they are not created by the app itself - how do i change the permissions accordingly? Saving the files in the app folder is not an option. The API version is 29.

package com.example.pos1;

import static com.example.example.pos1.*;

import static org.apache.commons.io.FilenameUtils.removeExtension;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import androidx.core.app.ActivityCompat;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity {

    private Button startnPButton;



    @Override
    protected void onCreate(Bundle savedInstanceState) {

        requestStoragePermission();


        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        startnPButton = (Button) findViewById(R.id.nPButton);
        startnPButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                openNew();
            }
        });


        ImportMA();



    }

    private void requestStoragePermission()
    {
     
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE}, PackageManager.PERMISSION_GRANTED);



    }

    public void openNew() {
        Intent intent = new Intent(this, neuesProjekt.class);
        startActivity(intent);
    }

    public void loadexisting() {
        Intent intent = new Intent(this, NeueWohnung.class);
        startActivity(intent);


    }

    public void ImportMA()
    {

        ListView listView=findViewById(R.id.listview);

        List<String> list = new ArrayList<>();

        ArrayAdapter arrayAdapter = new ArrayAdapter(getApplicationContext(), android.R.layout.simple_list_item_1,list);

        File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);
        File[] files = path.listFiles();

        

        if (files.length>0)
        {

            for(int i = 0; i < files.length; i++)
            {
                System.out.println(files[i].getName());
                list.add(removeExtension(files[i].getName()));

            }

        }
        else
        {
            list.add("No elements!");

        }
        listView.setAdapter(arrayAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {

                String x = (String) listView.getItemAtPosition(position);
                String filepath = new String (Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS)+"/"+x+".xls");
                setFilepath(filepath);
                setName(x);

                Toast.makeText(getApplicationContext(),
                        "Projekt: "+getName(), Toast.LENGTH_SHORT)
                        .show();



                loadexisting();

            }


        });

    }


}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source