How to get list of video files in a specific folder in android?

后端 未结 3 996
旧时难觅i
旧时难觅i 2020-12-30 12:06

How to get list of video files stored in a specific folder using mediastore? currently i\'m using this code

package com.example.videolisttest;

import androi         


        
3条回答
  •  执念已碎
    2020-12-30 12:46

    private List path_vid;
    public void searchVid(File dir) {
        String pattern = ".mp4";
                //Get the listfile of that flder
        final File listFile[] = dir.listFiles();
    
        if (listFile != null) {
            for (int i = 0; i < listFile.length; i++) {
                final int x = i;
                if (listFile[i].isDirectory()) {
                    walkdir(listFile[i]);
                } else {
                    if (listFile[i].getName().endsWith(pattern)) {
                        // Do what ever u want, add the path of the video to the list
                           path_vid.add(listFile[i]);
                    }
                }
            }
        }
    }
    

    This function is recursive, and search vids setting it into a list, the vids are searched from an especified folder, if u want to search vids into the sdCard, use:

    File sdCard = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
    //For example:
    //File vidsFolder= new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/Videos");
    searchVid(sdCard);
    if(path_vid.size()>0){
       //Convert list into array
       String[] array = path_vid.toArray(new String[path_vid.size()]);
       //Create Adapter
       ArrayAdapter adapter =new ArrayAdapter(this,android.R.layout.simple_list_item, array);
       //Set adapter to videlist
       videolist.setAdapter(adapter);
    }else{
       //No vids found
       exit();
    }
    

提交回复
热议问题