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());
}
}
}
To find downloads on your PC: Select File Explorer from the taskbar, or press the Windows logo key + E. Under Quick access, select 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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With