I use the following method to scan and index video files and pass data to an array. I get video file duration, file name, files size.

Is there any better method, or do I need to improve?

public static void load_Directory_Files(File directory) {
    File[] fileList = directory.listFiles();
    if(fileList != null && fileList.length > 0){
        for (int i=0; i<fileList.length; i++){
            } else {
                String name = fileList[i].getName().toLowerCase();
                for (String extension: Constant.videoExtensions){
                    //check the type of file
                        String FileName = fileList[i].getName();
                        String size = Function.humanReadableByteCountSI(fileList[i].length());
                        String duration = Function.getDurationFF(fileList[i].getAbsolutePath());
                        String folderName = fileList[i].getParentFile().getName();
                        String path = fileList[i].getAbsolutePath();
                        Log.d(TAG, "load_Directory_Files: "+ duration + "" +size + "" + FileName);
                        Constant.allMediaListWithModel.add(new AllVideoModel(FileName, path, folderName, size, duration));
                        //when we found file

public static String getDurationFF(String absolutePathThumb) {
        FFmpegMediaMetadataRetriever retriever = new FFmpegMediaMetadataRetriever();
        String time = retriever.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_DURATION);
        long timeInMillisec = Long.parseLong(time);
        return convertMillieToHMmSs(timeInMillisec);
    }catch(Exception e){
        return null;

public static String convertMillieToHMmSs(long millie) {
    long seconds = (millie / 1000);
    long second = seconds % 60;
    long minute = (seconds / 60) % 60;
    long hour = (seconds / (60 * 60)) % 24;
    if (hour > 0) {
        return String.format("%02d:%02d:%02d", hour, minute, second);
    } else {
        return String.format("%02d:%02d" , minute, second);

public static String humanReadableByteCountSI(long bytes) {
    if (-1000 < bytes && bytes < 1000) {
        return bytes + " B";
    CharacterIterator ci = new StringCharacterIterator("kMGTPE");
    while (bytes <= -999_950 || bytes >= 999_950) {
        bytes /= 1000;
    return String.format("%.1f %cB", bytes / 1000.0, ci.current());

The above method works but it takes time to scan and index. So how can I reduce indexing time?

public static String[] videoExtensions = {".mp4",".ts",".mkv",".mov",
        ".bup", ".dv",".flv",".m1v",".m2ts",".mpeg4",".vlc",".3g2",
  • 2
    \$\begingroup\$ Please do not update the code in your question to incorporate feedback from answers, doing so goes against the Question + Answer style of Code Review. This is not a forum where you should keep the most updated version in your question. Please see what you may and may not do after receiving answers. \$\endgroup\$
    – Vogel612
    Oct 26, 2020 at 14:11

1 Answer 1



The method name load_Directory_Files is a bit odd. To follow general Java naming conventions, it would be something like loadDirectoryFiles. This is called camel case.

File filtering

You can use a FileFilter to accept or discard files for the listFiles call, no need to do it yourself:

import java.io.File;
import java.io.FileFilter;
import java.util.List;
import java.util.Arrays;

public class VideoOrDirectoryFilter implements FileFilter {

    private static final List<String> ALLOWED_EXTENSIONS = Arrays.asList(
        ".mpeg1",".mpeg2",".mts",".ogm", ".bup", ".dv",".flv",

    public boolean accept(File file) {
        return file.isDirectory() 
            || ALLOWED_EXTENSIONS.contains(file.getName().toLowerCase());

Null check

In loadDirectoryFiles, you can return early if the listFiles call is null:

File[] files = directory.listFiles(new VideoOrDirectoryFilter());
if (files == null) return;


Instead of the for loop with an index variable, you could use a foreach loop. That makes for cleaner code, as you directly get the object in the list. Also, you don't need to check the length of the list before looping. If it's empty, the loop will be a no-op.

for (File file : files) {
    // Do your thing with file here


Here's a reimplementation of the loadDirectoryFiles method with the above enhancements included.

public static void loadDirectoryFiles(File directory) {
    File[] files = directory.listFiles(new VideoOrDirectoryFilter());

    if (files == null) return;

    for (File file : files) {
        if (file.isDirectory()){
        } else {            
            String fileName = file.getName();
            String size = Function.humanReadableByteCountSI(file.length());
            String duration = Function.getDurationFF(file.getAbsolutePath());
            String folderName = file.getParentFile().getName();
            String path = file.getAbsolutePath();
            Log.d(TAG, "load_Directory_Files: "+ duration + "" +size + "" + fileName);
            Constant.allMediaListWithModel.add(new AllVideoModel(fileName, path, folderName, size, duration));
  • \$\begingroup\$ This is quite good. The only thing I'd suggest changing is "load_Directory_Files: "+ duration + "" +size + "" + fileName - that should just be a format call. \$\endgroup\$
    – Reinderien
    Nov 3, 2020 at 14:54
  • \$\begingroup\$ i tried but but no files are listed, here is how i implement gist.github.com/sanoj26692/5e5e9baedf0d3821213042581d248fc4 \$\endgroup\$ Nov 5, 2020 at 14:42
  • \$\begingroup\$ Can you make a github. Bcoz I tried ur code and that doesn't index any files \$\endgroup\$ Nov 21, 2020 at 17:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.