简体   繁体   中英

List files inside Download directory in android

I am creating my own application for android and I have a problem. I need to get a list of all files from the downloads directory, I tried to do it like this:

String path = mContext.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).toString();

                File fl  = new File(path);
                File[] files = fl.listFiles();
                Log.i("File", String.valueOf(files.length));

                for(int i = 0; i < files.length; i++)
                {
                    Log.i("Files", files[i].getName());
                }

But it shows me that there are no files in this directory, although there are files there. In advance, I can say that I added permission to the manifest:

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

And it still shows me that there are no files in this directory, maybe someone can tell me how to solve this problem. (I'm using android 11 device)

For reading, you just need Read permission:

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

And for getting download directory you can use getExternalStoragePublicDirectory as following:

private void listFiles() {

        File fl = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
        File[] files = fl.listFiles();
        Log.i("File", String.valueOf(files.length));

        for (int i = 0; i < files.length; i++) {
            Log.i("Files", files[i].getName());
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM