Wednesday, 12 March 2014

Display list of files with particular extension like(.txt,.png,etc)

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..:)

0 comments:

Post a Comment

 

Copyright © Unique Android Tutorial.. Design by Free CSS Templates | Blogger Theme by BTDesigner | Powered by Blogger