0

I am developing an app in which I am generating the list of all mp3 files and adding it in a custom list. I have a program for this but the problem is that it doesn't return the files inside the subfolder.

Can anyone advise me or give a link or something so that I can do it iteratively? I want to do it iteratively because if I don't do so, I will have to pass a lot of information between methods and it will become really confusing. I tried doing that and ended up totally confused.

Here is the code:

public class FragmentSongs extends Fragment {

    private static final String Sd_Path=new String("/sdcard/");
    private MediaPlayer mp =new MediaPlayer();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_a, container, false);
        ListView SngList = (ListView) view.findViewById(R.id.SongList);

           ArrayList<SongDetails> Songinfo;
             // AdapterView.AdapterContextMenuInfo info;
              Songinfo = new ArrayList<SongDetails>();

            File f=new File("/sdcard/");
            File[] files = f.listFiles(new Mp3Filter());
            if( files.length>0)
            {
                for(int i=0; i<files.length; i++) 
                {SongDetails detail=new SongDetails();
                    detail.setIcon(R.drawable.ic_launcher);
                 detail.setSong(files[i].getName());
                 detail.setArtist(files[i].getName());
                 detail.setAlbum(files[i].getName());
                 Songinfo.add(detail);
                }  SngList.setAdapter(new CustomAdapter(Songinfo ));
            }
            else  if (files.length == 0)
                return null;
        return view;
    } }
class Mp3Filter implements FilenameFilter{
    public boolean accept(File dir,String name)
    {
    return (name.endsWith(".mp3"))|| (name.endsWith(".Mp3")) ||(name.endsWith(".MP3"));//searching for the files
    }
}
ajm475du
  • 361
  • 1
  • 5
  • Hey means you want to built something like File manager to access files? – Looking Forward Aug 02 '13 at 16:37
  • not exactly,i just want it to return the files that i wan't (in a list) my code searches the files only in the folder i am providing,not in its subfolder,there exists a recursive version of it,involving a separate method,but i don't want to use a separate method for it,as mentioned above –  Aug 02 '13 at 16:39

1 Answers1

0

Add a separate method to your activity that does the following:

private ArrayList<SongDetails> getSongsFromDirectory(File f){
    ArrayList<SongDetails> songs = new ArrayList<SongDetails>();
    if (!f.exists() || !f.isDirectory()) return songs;

    File[] files = f.listFiles(new Mp3Filter());

    for (File file : files){
       if (file.isDirectory()){
          songs.addAll(getSongsFromDirectory(file));
       }
       else{
          SongDetails detail=new SongDetails();
          detail.setIcon(R.drawable.ic_launcher);
          String fileName = file.getName();
          detail.setSong(fileName);
          detail.setArtist(fileName);
          detail.setAlbum(fileName);
          songs.add(detail);
       }
    }

    return songs;
}

Also change your filter to a FileFilter and also return folders

class Mp3Filter implements FileFilter{
    public boolean accept(File file){
       return (file.isDirectory() || file.getName().toUpperCase().endsWith(".MP3"))
    }
}

Finally, change your code in onCreateView to this:

View view = inflater.inflate(R.layout.fragment_a, container, false);
ListView SngList = (ListView) view.findViewById(R.id.SongList);
File f=new File("/sdcard/");
ArrayList<SongDetails> Songinfo = getSongsFromDirectory(f);
if (songinfo.size()>0){
   SngList.setAdapter(new CustomAdapter(Songinfo));
   return view;
}
else return null;

Please note that I did not test this code, but it should at least give you an idea of how to accomplish it.

FD_
  • 12,947
  • 4
  • 35
  • 62
  • what happens to the variable "i"? –  Aug 02 '13 at 17:12
  • Right, sorry, I'll fix that. – FD_ Aug 02 '13 at 17:12
  • it crashes, i had to reinitialize the variable "f" in your method File f=new File("/sdcard/"); because it was a local variable........i hope that's not creating an issue??thats why i was avoiding seperate methods. –  Aug 02 '13 at 17:21
  • There was another mistake in the code ;) I've fixed it now. Please note the name of the argument getSongsFromDirectory takes. – FD_ Aug 02 '13 at 17:26
  • yeah i noticed that,i was thinking of doing the same,i am not good with passing info between methods,thanx for your time though,letme c if it works. –  Aug 02 '13 at 17:27
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/34712/discussion-between-fd-and-ankit) – FD_ Aug 02 '13 at 17:32