What is a good way to get a list of files from (a directory on) the sd card?

前端 未结 3 960
悲哀的现实
悲哀的现实 2020-12-10 06:41

I\'m happy reading and writing to a pre-set file, and could manually populate a listview, but I\'m hoping there is an official(or not) filebrowser I missed, or other more el

3条回答
  •  甜味超标
    2020-12-10 07:06

    Code : listing all the music files or documents files or videos files , images , in android from external storage to listView.

    ArrayList filenames;
    ArrayAdapter adapter;
    

    // onCreate method

    songlist =  findViewById(R.id.songlist);  //songlist is ListView widget
        filenames = new ArrayList<>();
        getMusic();         // music files funtions
        adapter=new ArrayAdapter<>(YourActivity.this, android.R.layout.simple_list_item_1, filenames);
        songlist.setAdapter(adapter);
    

    // getMusic() is method for getting and filtering all music files from external storage

    private void  getMusic() {
    
        File path = Environment.getExternalStorageDirectory();
        String filename ;
        Queue files = new LinkedList<>();         //Linklist
    
        files.addAll(Arrays.asList(path.listFiles()));  //adding all files of path in linklist
    
        while (!files.isEmpty()){
            File file = files.remove();
            if (file.isDirectory()){
                files.addAll(Arrays.asList(file.listFiles()));
    
    
            }
            else if (file.getName().endsWith(".mp3")){   //filtering files
                filename = file.getName();               // according to their extension
                filenames.add(filename);              //filenames is string ListArray
    
            }
            else if (file.getName().endsWith(".ogg")){
                filename = file.getName();
                filenames.add(filename);
            }
            else if (file.getName().endsWith(".amr")){
                filename = file.getName();
                filenames.add(filename);
            }
            else if (file.getName().endsWith(".wav")){
                filename = file.getName();
                filenames.add(filename);
            }
            else if (file.getName().endsWith(".wma")){
                filename = file.getName();
                filenames.add(filename);
            }
            else if (file.getName().endsWith(".flac")){
                filename = file.getName();
                filenames.add(filename);
            }
    
    
        }
    
    
    }
    

    just change string values like (.mp3 , .wav) to .your_file_extension

提交回复
热议问题