Akarsh M
Akarsh M

Reputation: 1611

How can I create a listView of all TEXT file from my sdcard?

what I need :

1) I need to find all TEXT file which is my sdcard folder/sub folder also.

2)After than I store that all file name and path store into the ArrayList.

So, first I have to sort the first point. What classes are required for this ?

What I search:

1)FilenameFilter but not getting proper way to use this

Upvotes: 0

Views: 866

Answers (2)

GrIsHu
GrIsHu

Reputation: 23638

Try out below code to get all the text files from sdcard.

        private txtFileFilter txtff = new txtFileFilter(); 
         private void scanAllTxtFiles(File location, ArrayList<String> list) {
                    File[] files = location.listFiles(txtff);
                    if (files != null) {
                        for (File f : files) {
                            if (f.isDirectory()) {
                                scanAllTxtFiles(f, list);
                            } else if (f.isFile()) {
                                list.add(f.getAbsolutePath().substring(
                                        externalPathLength));
                            }
                        }
                    }
                }
            }
      private class txtFileFilter implements FileFilter {
                @Override
                public boolean accept(File pathname, String filename) {
                    // TODO Auto-generated method stub
                    if (filename.endsWith(".txt")) {
                        return true;
                    } 
                    return false;
                }
            }

Upvotes: 1

Biraj Zalavadia
Biraj Zalavadia

Reputation: 28484

Try this will give you a all text files from your sdcard

private ArrayList<String> allImages = new ArrayList<String>();
    String[] extensions = { "txt" };

    private void loadAllImages(String rootFolder) {

        File file = new File(rootFolder);
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            if (files != null && files.length > 0) {
                for (File f : files) {
                    if (f.isDirectory()) {
                        loadAllImages(f.getAbsolutePath());
                    } else {
                        for (int i = 0; i < extensions.length; i++) {
                            if (f.getAbsolutePath().endsWith(extensions[i])) {
                                allImages.add(f.getAbsolutePath());
                            }
                        }
                    }
                }
            }
        }

    }

Upvotes: 0

Related Questions