I would like to create a button that when clicked will go to a class that displays all media files from an SD card using a ListView.
After selecting fro
Add a Method GetFiles() to your program. Call it to get an ArrayList<> of all the files. You can then use it to populate your listview. You need to provide String argument DirectoryPath.
The Function:
public ArrayList GetFiles(String DirectoryPath) {
ArrayList MyFiles = new ArrayList();
File f = new File(DirectoryPath);
f.mkdirs();
File[] files = f.listFiles();
if (files.length == 0)
return null;
else {
for (int i=0; i
Usage Example:
@Override
public void onCreate() {
// Other Code
ListView lv;
ArrayList FilesInFolder = GetFiles("/sdcard/somefolder");
lv = (ListView)findViewById(R.id.filelist);
lv.setAdapter(new ArrayAdapter(this,
android.R.layout.simple_list_item_1, FilesInFolder));
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView> parent, View v, int position, long id) {
// Clicking on items
}
});
}
Make sure that the External Storage is Readable: http://developer.android.com/guide/topics/data/data-storage.html#filesExternal
To Filter files based on Name/Extension: How to acces sdcard and return and array with files off a specific format?