List the files in Download directory of the Android phone
Solution 1
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.
Solution 2
Add storage permission to your project manifest file:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
and ask the user to grant the permission like this:
int PERMISSION_ALL = 1;
String[] PERMISSIONS = {android.Manifest.permission.READ_EXTERNAL_STORAGE, android.Manifest.permission.WRITE_EXTERNAL_STORAGE};
add this method to your code:
public static boolean hasPermissions(Context context, String... permissions) {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
for (String permission : permissions) {
if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
}
return true;
}
and it is optional to override this:
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case 1: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// contacts-related task you need to do.
Toast.makeText(getApplicationContext(), "permission was granted", Toast.LENGTH_SHORT).show();
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
Toast.makeText(getApplicationContext(), "Permission denied", Toast.LENGTH_SHORT).show();
}
return;
}
}
}
now you can use the below code to access your application Download directory:
context.getApplicationContext().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
Good Luck :)
Admin
Updated on June 05, 2022Comments
-
Admin almost 2 years
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 returnsnull
.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()); } } }