Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import android.net.Uri;

import java.io.*;

import java.text.SimpleDateFormat;
import java.util.*;

public class Main {
    public static String prepareFilePathForVideoSaveWithDraftUri(Uri draftUri) {
        String draftPath = draftUri.getPath();
        String draftMediaDirPath = draftPath.substring(0, draftPath.length() - 5);
        File draftMediaDir = new File(draftMediaDirPath);
        if (!draftMediaDir.exists()) {
            draftMediaDir.mkdirs();
        }
        String[] files = draftMediaDir.list(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String filename) {
                return filename.endsWith("-0.mp4") || filename.endsWith("-a.mp4");
            }
        });
        List<String> filePaths = Arrays.asList(files);
        Collections.sort(filePaths, new Comparator<String>() {
            @Override
            public int compare(String lhs, String rhs) {
                return rhs.compareTo(lhs);
            }
        });
        if (filePaths.size() > 0) {
            for (String file : filePaths) {
                return new File(draftMediaDir, file.substring(0, file.length() - 6) + ".mp4").getAbsolutePath();
            }
        }
        return new File(draftMediaDir, generateRandomFilename("mp4")).getAbsolutePath();
    }

    public static String generateRandomFilename(String ext) {
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd_HHmmss_SSS");
        return df.format(new Date()) + "." + ext;
    }
}