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

前端 未结 3 952
悲哀的现实
悲哀的现实 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<String> filenames;
    ArrayAdapter<String> 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<File> 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

    0 讨论(0)
  • 2020-12-10 07:15
    final String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
    File[] files = Environment.getExternalStorageDirectory().listFiles();
    } else {
    ...
    }
    
    0 讨论(0)
  • 2020-12-10 07:23
    File fileList = new File("/sdcard");
    if (fileList != null){
        File[] filenames = fileList.listFiles();
            for (File tmpf : filenames){
                //Do something with the files
            }
        }
    }
    

    Is another method you can try

    0 讨论(0)
提交回复
热议问题