In this post we look into how to get filtered list with particular extension from sdcard or anywhere.
Please follow the below steps to do this:-
1)Write a method like following which simply returns a filtered list so you will used these anywhere or display this in a list view.
public ArrayList<String> File_Select()
{
File dir_home = new File(Environment.getExternalStorageDirectory(), "");
ArrayList<String> filteredlist = new ArrayList<String>();
if (dir_home .listFiles(new NameFilter()).length > 0)
{
for (File file : (dir_home .listFiles(new NameFilter()))
{
filteredlist .add(file.getName().substring(0, (file.getName().length() - 4)));
}
}
return filteredlist ;
}
2)Finally,write a class that will perform this filtering
class NameFilter implements FilenameFilter
{
public boolean accept(File dir, String name)
{
return (name.endsWith(".txt") ||name.endsWith(".TXT"));
}
}
Done.Thanks..Android Rocks..:)
Please follow the below steps to do this:-
1)Write a method like following which simply returns a filtered list so you will used these anywhere or display this in a list view.
public ArrayList<String> File_Select()
{
File dir_home = new File(Environment.getExternalStorageDirectory(), "");
ArrayList<String> filteredlist = new ArrayList<String>();
if (dir_home .listFiles(new NameFilter()).length > 0)
{
for (File file : (dir_home .listFiles(new NameFilter()))
{
filteredlist .add(file.getName().substring(0, (file.getName().length() - 4)));
}
}
return filteredlist ;
}
2)Finally,write a class that will perform this filtering
class NameFilter implements FilenameFilter
{
public boolean accept(File dir, String name)
{
return (name.endsWith(".txt") ||name.endsWith(".TXT"));
}
}
Done.Thanks..Android Rocks..:)


