Show last created audio file in Android

253 views Asked by At

I am using following code to find last created files.

public class MainActivity extends AppCompatActivity {

Button button;
TextView textView;
ArrayList<File> fileArrayList;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    fileArrayList = new ArrayList<>();

    textView = (TextView) findViewById(R.id.tv);
    final String dir = String.valueOf(Environment.getExternalStorageDirectory());
    Log.d("Directory Path",dir);

    fileArrayList = listf(dir,fileArrayList);

    button = (Button) findViewById(R.id.btn);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            File lastModified = lastFileModified(fileArrayList);
            textView.setText(lastModified.getPath());
        }
    });




}

public static File lastFileModified(ArrayList<File> fileArrayList) {

    long lastMod = Long.MIN_VALUE;
    File choice = null;
    for (File file : fileArrayList) {


        if (file.lastModified() > lastMod) {
            choice = file;
            lastMod = file.lastModified();
        }
    }
    return choice;
}

public ArrayList<File> listf(String directoryName, ArrayList<File> files) {
    File directory = new File(directoryName);

    // get all the files from a directory
    File[] fList = directory.listFiles();
    for (File file : fList) {
        if (file.isFile()) {
            files.add(file);
        } else if (file.isDirectory()) {
            listf(file.getAbsolutePath(), files);
        }
    }

    return files;
}

}

The above code gives me some random last created file. But I want last created audio file. Also the audio file created should be most recent either from Internal storage or external storage. Please help. Thank You.

1

There are 1 answers

0
kwaghela On BEST ANSWER

I took a look at your code. Please do the following changes, it works for me.

public class MainActivity extends AppCompatActivity {


File lastModifiedFile;
TextView tv;
ArrayList<File> files;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tv = (TextView) findViewById(R.id.tv);

    files = new ArrayList<>();

    String d = String.valueOf(Environment.getExternalStorageDirectory());
    files = listf(d, files);

    File finalmodified = checkLastmod(files);
    tv.setText(finalmodified.getAbsolutePath());
}

private ArrayList<File> listf(String d, ArrayList<File> files) {
    File directory = new File(d);

    // get all the files from a directory
    File[] fList = directory.listFiles(new AudioFilter());
    for (File file : fList) {
        if (file.isFile()) {
            files.add(file);
        } else if (file.isDirectory()) {
            listf(file.getAbsolutePath(), files);
        }
    }
    return files;
}

private File checkLastmod(ArrayList<File> audiofiles) {
    long latmod = Long.MIN_VALUE;
    lastModifiedFile = null;
    for(File file: audiofiles)
    {
        if(file.lastModified() > latmod){
            lastModifiedFile = file;
            latmod = file.lastModified();
        }
    }
    return lastModifiedFile;
}


public class AudioFilter implements FileFilter {

    // only want to see the following audio file types
    private String[] extension = {".aac", ".mp3", ".wav", ".ogg", ".midi", ".3gp", ".mp4", ".m4a", ".amr", ".flac"};

    @Override
    public boolean accept(File pathname) {

        // if we are looking at a directory/file that's not hidden we want to see it so return TRUE
        if (pathname.isDirectory() && !pathname.isHidden()) {
            return true;
        }

        // loops through and determines the extension of all files in the directory
        // returns TRUE to only show the audio files defined in the String[] extension array
        for (String ext : extension) {
            if (pathname.getName().toLowerCase().endsWith(ext)) {
                return true;
            }
        }

        return false;
    }
}}