#StackBounty: #java #android Scanning and indexing files video in android java

Bounty: 50

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

is there any better method to do ?, or do i need to improve a better way ?.

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++){
            if(fileList[i].isDirectory()){
                load_Directory_Files(fileList[i]);
            } else {
                String name = fileList[i].getName().toLowerCase();
                for (String extension: Constant.videoExtensions){
                    //check the type of file
                    if(name.endsWith(extension)){
                        Constant.allMediaList.add(fileList[i]);
                        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
                        break;
                    }
                }
            }
        }
    }
}



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

@SuppressLint("DefaultLocale")
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);
    }
}

@SuppressLint("DefaultLocale")
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;
        ci.next();
    }
    return String.format("%.1f %cB", bytes / 1000.0, ci.current());
}

Above method work but it takes time to scan and index. So how can i reduce indexing time, or is there anything to improve.

public static String[] videoExtensions = {".mp4",".ts",".mkv",".mov",
        ".3gp",".mv2",".m4v",".webm",".mpeg1",".mpeg2",".mts",".ogm",
        ".bup", ".dv",".flv",".m1v",".m2ts",".mpeg4",".vlc",".3g2",
        ".avi",".mpeg",".mpg",".wmv",".asf"};


Get this bounty!!!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.