set a variable to a the folder path Environment.DIRECTORY_DOWLOAD in android

2k views Asked by At

I can't seem to find an answer to this simple question.

How do I set a variable to contain the path string to the internal download directory of my app?

root = Environment.DIRECTORY_DOWLOAD;

Then I could access files within that directory by:

root+"/"+filename+".pdf"

I can open a file list but I don't understand what it is doing

public void showDownload(View view) {
    Intent i = new Intent();
    i.setAction(DownloadManager.ACTION_VIEW_DOWNLOADS);
    startActivity(i);
}
1

There are 1 answers

7
Simas On BEST ANSWER

If all you want to do is list the files inside that directory you can do it like this:

File dir = Environment.getExternalStoragePublicDirectory(Environment
        .DIRECTORY_DOWNLOADS);
String dirPath = dir.getAbsolutePath();

String[] files = dir.list();
for (String file : files) Log.e("", "File: " + file);

Or like:

File[] files = dir.listFiles();
for (File file : files) Log.e("", "File: " + file.getAbsolutePath());