Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List the files in Download directory of the Android phone

I am using the following code for trying to list the files in Download directory.

I build the apk, install it on my phone and then run it. I have files in both Internal memory/Download folder and External memory/Download folder when I view in File Browser of my phone, but the app does not display the file list. When I debug I find that the listFiles() function returns null.

Please let me know where I am doing wrong. The variable state has value mounted so the issue has nothing to do with the memory not being mounted.

String state = Environment.getExternalStorageState();

private boolean isMediaAvailable() {

    if (Environment.MEDIA_MOUNTED.equals(state)) {
        return true;
    } else {
        return false;
    }
}


if (!isMediaAvailable()) {
        Utility.finishWithError(this,"Media Not Available");
    } else {

        String path =Environment.getExternalStorageDirectory().toString()+ File.separator + Environment.DIRECTORY_DOWNLOADS;



        File file = new File(path);
        mRootPath = file.getAbsoluteFile().getPath();



        mFileNames = new ArrayList<String>();

        File filesInDirectory[] = file.listFiles();


        if (filesInDirectory != null) {
            for (int i = 0; i<filesInDirectory.length;i++) {
                    mFileNames.add(filesInDirectory[i].getName());
            }
        }

    }
like image 751
Nagarajan P Avatar asked May 16 '15 23:05

Nagarajan P


People also ask

How do I find a list of my Downloads?

To find downloads on your PC: Select File Explorer from the taskbar, or press the Windows logo key + E. Under Quick access, select Downloads.

What is the directory for Downloads?

By default, Chrome, Firefox and Microsoft Edge download files to the Downloads folder located at %USERPROFILE%\Downloads. USERPROFILE is a variable that refers to the logged in user's profile directory on the Windows computer, e.g. the path may look like C:\Users\YourUserName\Downloads.


1 Answers

Use :

Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)

to get the downloaded directory,

and use File.list() to get an array with the list of files in the directory.

like image 124
Christian Avatar answered Sep 29 '22 17:09

Christian