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

后端 未结 3 979
旧时难觅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<String> 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<String> adapter =new ArrayAdapter<String>(this,android.R.layout.simple_list_item, array);
       //Set adapter to videlist
       videolist.setAdapter(adapter);
    }else{
       //No vids found
       exit();
    }
    
    0 讨论(0)
  • 2020-12-30 12:50

    you can use this code for get videos from specific folder as:

      String selection=MediaStore.Video.Media.DATA +" like?";
                String[] selectionArgs=new String[]{"%FolderName%"};
                videocursor = managedQuery(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
                        parameters, selection, selectionArgs, MediaStore.Video.Media.DATE_TAKEN + " DESC");
    

    Its works for me like a charm.

    0 讨论(0)
  • 2020-12-30 12:53

    If you know the specific folder, use:

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