0

Can someone please point out whats going wrong here:

The code below loops through the directories on the /sdcard. If it finds a file it adds the name etc to an array. If it finds a directory, it drops in and scans it.

The system.out returns all of the files on the /sdcard.

The array adapter only lists what it finds at the top level directory

On my sd card I am expecting it to return two mp3 files, one in the top level directory, one burried within the tree. The system.out line finds both.

I suspect that the fls arraylist is being destroyed. If it is, how do I go about preserving this?

Thanks in advance!

 public void fill(File f) {     
        File[]files = f.listFiles();
          List<Option>fls = new ArrayList<Option>();
          if (files != null) {
                for(File ff: files){
                    if(!ff.isDirectory())
                        {
                        /** Only Add MP3's to the list    */
                        String filename=ff.getName();
                        String ext = filename.substring(
                                    filename.lastIndexOf('.')+1, filename.length());
                                           if(ext.equals("MP3")||ext.equals("mp3")||ext.equals("Mp3")||ext.equals("mP3"))
                        fls.add(new Option(ff.getName(),"File Size: "+ff.length()/1048576 + "MB",ff.getAbsolutePath()));
                        System.out.println( ff.getName() );
                        }
                    else
                        fill(ff);
               }    
          }

        Collections.sort(fls);
        adapter = new FileArrayAdapter(getActivity(),R.layout.folder_view,fls);
        this.setListAdapter(adapter);   
        }

1 Answer 1

1

With the code as it is the arraylist will be recreated everytime you enter a new folder, you need to take the adapter code out of the function and have the fill function take the arraylist as a parameter so that each call will take the same arraylist.

ie.

List<Option>fls = new ArrayList<Option>();

fill(f,fls);

Collections.sort(fls);
adapter = new FileArrayAdapter(getActivity(),R.layout.folder_view,fls);
this.setListAdapter(adapter); 

if you want to keep it down to one call(which i dont recommend), you can do

fill(f,null);

and in the fill function check if the arraylist is null, if it is initialize it, but you must still pass it to additional calls.

Sign up to request clarification or add additional context in comments.

3 Comments

Hi,thanks for the reply. I've made the change so this now works as I'd hoped. It is however quite slow at the top folder level, where it is effectively scanning the entire sdcard. It takes about 20 seconds to build the list. Any suggestions for optimizing this? I may simply stick this function on a button or long press action, unless it can be optimised significantly.
The only optimization i can think of is doing you checking file extension a little different. By, if(ff.getName().toLowerCase().endsWith("mp3"); But you would need to test to see speed difference. Most likely you would need to implement this in an asynctask of sorts, i doubt it can be fast enough to where you would not need to inform the user.
oh nice, that tidies the code up if nothing else, so I'll drop that in anyway. I have it set up to skip hidden directories, as well as a list of standard dirs (DCIM etc) to skip. The alternative is to ask the user to select a folder and store that. Having said all that, all of the music is within my /Music folder, and that takes just as long to scan as the level above, so that won't help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.