1

I have already looked at these questions on stackoverflow:

http://stackoverflow.com/questions/22332957/display-media-files-in-listview-nullpointerexception
http://stackoverflow.com/questions/9317483/showing-a-list-of-files-in-a-listview
http://stackoverflow.com/questions/5800981/how-to-display-files-on-the-sd-card-in-a-listview
http://stackoverflow.com/questions/9782168/how-to-show-audio-files-in-a-listview-in-android

But none of them worked for my code. I am not sure why it doesn't work?

What I am trying to do is to create a listview that displays all of the files inside a specific folder. The specific folder I want to be displayed is the Download folder (the one you can see if you plug your phone into your USB). To clarify, on my PC, the directory looks like this:

This PC\Nexus 4\Internal storage\Download

Here is my code (in my onCreate method):

arraylist = new ArrayList<String>();
listview = (ListView) findViewById(R.id.list);


File file = new File(Environment.DIRECTORY_DOWNLOADS);
File[] list = file.listFiles();

if (list.length == 0)
{
    Log.w("oh no", "oh no");
}
else {
    for( int i=0; i < list.length; i++)
    {
        arraylist.add( list[i].getName() );
    }
    }

adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1
            ,arraylist);

listview.setAdapter(adapter);

I have tried

File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString()); 

I have also tried

File directory = Environment.getExternalStorageDirectory();
file = new File(directory+ "/Download");// + "/Test");

Though it still doesn't work.

My log cat says that there is a nullpointerexception starting on the line that declares the for loop:

for( int i=0; i < list.length; i++)

Thank you very much in advance!

Cherry
  • 173
  • 1
  • 10
  • Oh, I also forgot to say that it did not even print out my log of "oh no" in logcat, which means that the array, "list" is not empty? (Which makes me even more confused!) – Cherry Mar 25 '14 at 23:06
  • it says that it returns a list of files or null listFiles() so try null instead of 0 – JRowan Mar 25 '14 at 23:13
  • Hello, the activity finally opens, but the listview is empty (which means my directory is wrong? I don't understand how to get the correct directory.) – Cherry Mar 25 '14 at 23:18
  • Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS); – JRowan Mar 25 '14 at 23:19
  • either that or theres no files in the download directory – JRowan Mar 25 '14 at 23:20
  • I have a ton of files in my download directory though, and yes I have tried what you gave me, yet it gave me the log that I typed (the "oh no"). In order to be sure, I would just like to repeat again that in my perspective, I am thinking of the downloads folder as: This PC\Nexus 4\Internal storage\Download – Cherry Mar 25 '14 at 23:23
  • Can you add log to print file.getPath() and see what is that returning – AnswerBot Mar 26 '14 at 04:27
  • Hello, I'm sorry for my late reply. It says: /storage/emulated/0/download. – Cherry Mar 26 '14 at 13:25
  • Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Albireo Mar 07 '16 at 08:55

1 Answers1

0

I have figured out exactly what I did that was wrong. What JRowan told me was correct. In order to get the directory of my device's Download folder, then I need to use this piece of code:

Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS); 

Initially, it didn't work because I forgot to add this permission to my Android manifest:

android.permission.WRITE_EXTERNAL_STORAGE

So, to summarize, I forgot to write this crucial permission. Without it, the directory actually gets me to: /storage/emulated/0/download (which isn't what I want obviously).

Thanks to the two people who tried to answer my question!

Cherry
  • 173
  • 1
  • 10