im new to android development so please forgive me if this is an easy thing to do.
i want to get all the files in a directory on the sd card and display them in a spinner, but i just cant work out how.
this is what i have and i dont even know if any of it is any good.
//creates this directory if its not there??
File sd = new File("/sdcard/myfolder");
//gets a list of the files
File[] sdDirList = sd.listFiles();
//add them to the spinner array (this makes it crash)
for(int i=0;i<sdDirList.length;i++)
array_spinnerLoad[i] = sdDirList[i].getName();
so where am i going wrong?
any usefull link to an easy to use tutorial anywhere?
it doesnt have to be a spinner, just some list i can select from
thank you 🙂
edit:
it doesnt crash with this but the spinner array is not filled
File[] sdDirList = sd.listFiles();
if (sdDirList != null)
{
array_spinnerLoad=new String[sdDirList.length];
for(int i=0;i<sdDirList.length;i++)
array_spinnerLoad[i] = sdDirList[i].getName();
}
If
/sdcard/myfolderdoesn’t exist or is not a directory, thenlistFilesreturnsnull. You’re probably crashing with a NPE. You can useexists()andisDirectory()to diagnose what’s wrong. (Plus, you can test thatsdDirList != null.)P.S. It would help you to get more accurate answers to post details about the crash — such as the exception.