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

℡╲_俬逩灬. 提交于 2019-11-30 10:17:35

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.

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();
}

If you know the specific folder, use:

.getExternalFilesDir
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!