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
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();
}
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.
If you know the specific folder, use:
.getExternalFilesDir