0

Possible Duplicate:
Android File Picker

I need to include a file chooser that returns me the full file path of the selected file, in my Android App.

But I have no idea of How I could implement this.

I have yet looking for this question in Stackoverflow but I haven't find a clear answer to my question.

I have find how to get filePath from images in the Gallery but nothing about a way to get the filePath of also all others file type.

Community
  • 1
  • 1
AndreaF
  • 11,975
  • 27
  • 102
  • 168

1 Answers1

0
    private List<String> FindFiles(Boolean fullPath) {
        final List<String> tFileList = new ArrayList<String>();

        String[] fileTypes = new String[]{"dat","doc","apk"....}; // file extensions you're looking for
        FilenameFilter[] filter = new FilenameFilter[fileTypes .length];

        int i = 0;
        for (final String type : fileTypes ) {
            filter[i] = new FilenameFilter() {
                public boolean accept(File dir, String name) {
                    return name.endsWith("." + type);
                }
            };
            i++;
        }

        FileUtils fileUtils = new FileUtils();
        File[] allMatchingFiles = fileUtils.listFilesAsArray(
                new File("/sdcard"), filter, -1);
        for (File f : allMatchingFiles) {
            if (fullPath) {
                tFileList.add(f.getAbsolutePath());
            }
            else {
                tFileList.add(f.getName());
            }
        }
        return tFileList;
    }

    public class FileUtils {


        public File[] listFilesAsArray(File directory, FilenameFilter[] filter,
                int recurse) {
            Collection<File> files = listFiles(directory, filter, recurse);

            File[] arr = new File[files.size()];
            return files.toArray(arr);
        }

        public Collection<File> listFiles(File directory,
                FilenameFilter[] filter, int recurse) {

            Vector<File> files = new Vector<File>();

            File[] entries = directory.listFiles();

            if (entries != null) {
                for (File entry : entries) {
                    for (FilenameFilter filefilter : filter) {
                        if (filter == null
                                || filefilter
                                        .accept(directory, entry.getName())) {
                            files.add(entry);
                            Log.v("FileUtils", "Added: "
                                    + entry.getName());
                        }
                    }
                    if ((recurse <= -1) || (recurse > 0 && entry.isDirectory())) {
                        recurse--;
                        files.addAll(listFiles(entry, filter, recurse));
                        recurse++;
                    }
                }
            }
            return files;
        }
    }
Tamir Scherzer
  • 995
  • 8
  • 8
  • How I should use this code? Could you give me more details? – AndreaF Oct 02 '12 at 01:06
  • sure, include this code in your activity. then use List files = FindFiles(true) to get the list of files. use "true" to get full path, and "false" to get just the file names. all files on your SDCARD with extentions included in fileTypes will be returned – Tamir Scherzer Oct 02 '12 at 07:59