how to get the file names stored in sd card in android

后端 未结 5 1990
日久生厌
日久生厌 2021-02-02 00:52


i have a folder in sd card which contains several files. now i need to get the names of that files. can anybody have any idea how to get the file names stored in s

5条回答
  •  名媛妹妹
    2021-02-02 01:46

    /**
     * Return list of files from path. 
     *
     * @param path - The path to directory with images
     * @return Files name and path all files in a directory, that have ext = "jpeg", "jpg","png", "bmp", "gif"  
     */
    private List getListOfFiles(String path) {
    
        File files = new File(path);
    
        FileFilter filter = new FileFilter() {
    
            private final List exts = Arrays.asList("jpeg", "jpg",
                    "png", "bmp", "gif");
    
            @Override
            public boolean accept(File pathname) {
                String ext;
                String path = pathname.getPath();
                ext = path.substring(path.lastIndexOf(".") + 1);
                return exts.contains(ext);
            }
        };
    
        final File [] filesFound = files.listFiles(filter);
        List list = new ArrayList();
        if (filesFound != null && filesFound.length > 0) {
            for (File file : filesFound) {
               list.add(file.getName());
            }
        }
    
        return list;
    }
    

    This will give you the list of images in a folder. You can modify the code to get all files.

提交回复
热议问题