Football Fans: Download the 2012 Schedule App from Google Play!


Go Back   Android Forums > Android Development > Application Development

Application Development Dev Lounge for the Coder Folks



Reply
 
LinkBack Thread Tools
Old December 3rd, 2011, 02:06 PM   #1 (permalink)
Junior Member
 
JohnLaird's Avatar
 
Join Date: Nov 2011
Location: Houston, TX
Posts: 20
 
Device(s): Motorola Razr
Thanks: 0
Thanked 3 Times in 3 Posts
Default trouble getting directory contents

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;
}

JohnLaird is offline  
Reply With Quote
Sponsors
Old December 3rd, 2011, 09:08 PM   #2 (permalink)
Junior Member
 
JohnLaird's Avatar
 
Join Date: Nov 2011
Location: Houston, TX
Posts: 20
 
Device(s): Motorola Razr
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Problem resolved. The solution if anyone is interested...

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

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

to this:

loadListOfFiles(Environment.getExternalStoragePubl icDirectory(Environment.DIRECTORY_MUSIC).getAbsolu tePath());
JohnLaird is offline  
Reply With Quote
Reply

Bookmarks


Go Back   Android Forums > Android Development > Application Development User CP
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -5. The time now is 11:51 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
Custom vBulletin Skins by: Relivo