December 3rd, 2011, 02:06 PM
|
#1 (permalink)
|
|
Junior Member
Join Date: Nov 2011
Location: Houston, TX
Posts: 20
Device(s): Motorola Razr
Thanks: 0
Thanked 3 Times in 3 Posts
|
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;
}
|
|
|