Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scan a file folder in android for file paths

Tags:

java

android

So i have a folder at "mnt/sdcard/folder" and its filled with image files. I want to be able to scan the folder and for each of the files that is in the folder put each file path in an arraylist. Is there an easy way to do this?

like image 303
Peter Avatar asked Mar 11 '11 23:03

Peter


People also ask

How do I track a file path?

Click the Start button and then click Computer, click to open the location of the desired file, hold down the Shift key and right-click the file. Copy As Path: Click this option to paste the full file path into a document. Properties: Click this option to immediately view the full file path (location).


2 Answers

You could use

List<String> paths = new ArrayList<String>();
File directory = new File("/mnt/sdcard/folder");

File[] files = directory.listFiles();

for (int i = 0; i < files.length; ++i) {
    paths.add(files[i].getAbsolutePath());
}

See listFiles() variants in File (one empty, one FileFilter and one FilenameFilter).

like image 124
David Lantos Avatar answered Oct 10 '22 13:10

David Lantos


Yes, you can use the java.io.File API with FileFilter.

File dir = new File(path);
FileFilter filter = new FileFilter() {
    @Override
    public boolean accept(File file) {
        return file.getAbsolutePath().matches(".*\\.png");
    }
};
File[] images = dir.listFiles(filter);

I was quite surprised when I saw this technique, as it's quite easy to use and makes for readable code.

like image 32
Matthew Willis Avatar answered Oct 10 '22 12:10

Matthew Willis