I am having some trouble displaying the files currently modified date.
public class MyAdapter extends ArrayAdapter {
String dir = \"/FileDire
In your question, you are pointing a Directory, not a File.
File file = new File(Environment.getExternalStorageDirectory() + dir);
private final Activity context;
Date lastModified = new Date(file.lastModified());
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
String formattedDateString = formatter.format(lastModified);
The idea is to get a Directory and iterate searching for the Last modified date of all files. The following question may help: How to get only 10 last modified files from directory using Java?
EDIT 1:
My Tricky Solution:
File images = new File("YourDirectoryPath");
long[] fileModifieDate = new long[images.listFiles().length];
int i=0;
File[] imagelist = images.listFiles(new FilenameFilter()
{
public boolean accept(File dir, String name)
{
File file = new File(dir, name);
fileModifieDate[i++] = file.lastModified();
return true;
}
});
// Here, max is the last modified date for this directory
// Here, Long array **fileModifieDate** will give modified time of all files, which you can also access from Files array
// if you want the last modified file in the directory you can do this:
File[] maxModifiedDate = images.listFiles(new FilenameFilter()
{
public boolean accept(File dir, String name)
{
File file = new File(dir, name);
return file.lastModified() == max;
}
});
// Now **maxModifiedDate** File array will have only one File, which will have max modified date.
EDIT 2:
For your case, this would be helpful:
public class MyAdapter extends ArrayAdapter<String> {
String dir = "/FileDirectory/";
File myFolder= new File(Environment.getExternalStorageDirectory() + dir);
if(myFolder.exists()){
File[] filelist = myFolder.listFiles(new FilenameFilter()
{
public boolean accept(File dir, String name)
{
return true;
}
});
}
// Now you have a filelist array of Files. If you want lastModified data, you can fetch from each individual file as you were doing previously:
private final Activity context;
Date lastModified = new Date(file.lastModified());
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
String formattedDateString = formatter.format(lastModified);
private String dateFormat = "dd/MM/yyyy HH:mm:ss";
Complete code to get the last modified date
File file = new File("c:\\test\\myfile.txt");
SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss");
System.out.println("Modified Date :- " + sdf.format(file.lastModified()));
File file = new File(Environment.getExternalStorageDirectory() + dir);
Points to directory, not a file.
If you need modified date of each file separately in a directory, just use walkfiletree.
Check this.
I tried the answer of Anonymous Mohit, but it only checks the file size of items inside the folder. But what if there are many subfolders? Here's how I did it.
public static long lastModifiedDate;
public static long getLastModified(String path) {
File directory = new File(path);
File[] files = directory.listFiles();
if(GlobalValues.DEBUG) Log.i(TAG, "Get last modified date: " + path);
if (files.length == 0) return directory.lastModified();
else {
for(File filesInFolder: files) {
if(filesInFolder.isDirectory()) {
getLastModified(filesInFolder.getAbsolutePath());
} else {
if(lastModifiedDate == 0) {
lastModifiedDate = filesInFolder.lastModified();
} else{
if(filesInFolder.lastModified() >= lastModifiedDate){
lastModifiedDate = filesInFolder.lastModified();
}
}
}
}
return lastModifiedDate;
}
}