• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps trouble getting directory contents

JohnLaird

Well-Known Member
I connected my android phone to my PC via USB cable and created a folder and file at "F:/Android/mymusic/so_happy.mp3". What I am trying to with my program on the phone is to list the contents of "F:/Android/mymusic/" (however that is represented on the device, I think it is something like /mnt/sdcard/Android/mymusic/ but that is not working). So far I am not getting anywhere. A listing of my code is posted below. Basically what it does is checks the directory for file contents, stores it in an arraylist of strings, and then is added to a spinner. So far, files is always null and "song0" and "song1" just get added to the spinner. What I want is to see "so_happy.mp3" there instead. Any help would be appreciated.

Code:
public class AndroidSimpleMusicPlayerActivity extends Activity 
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //get data for spinner
        filelist = new ArrayList<String>();
        loadListOfFiles(Environment.getExternalStorageDirectory().getAbsolutePath()+"Android/mymusic/");
        //load data for spinner
        ArrayAdapter <CharSequence> spinneradapter = new ArrayAdapter<CharSequence> (this, android.R.layout.simple_spinner_item );
        spinneradapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        for(int index=0; index<filelist.size(); index++)
            spinneradapter.add(filelist.get(index));
        Spinner songListSpinner = (Spinner) findViewById(R.id.selectSongSpinner);
        songListSpinner.setAdapter(spinneradapter);
    }
    private void loadListOfFiles(String path)
    {
        File f = new File(path);
        String[] files = f.list();

        TextView pathText = (TextView) findViewById(R.id.mypath);
        pathText.setText(path);
        
        if(files != null)
        {
            for(int index=0; index<files.length; index++)
                filelist.add(files[index]);
        }
        else
        {
            filelist.add("song0");
            filelist.add("song1");
        }
    }
    private ArrayList<String> filelist = null;
}
 
Problem resolved. The solution if anyone is interested...

Switched USB connection to PC-Mode, created "Music" directory and changed this line

loadListOfFiles(Environment.getExternalStorageDirectory().getAbsolutePath()+"Android/mymusic/");

to this:

loadListOfFiles(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC).getAbsolutePath());
 
Back
Top Bottom