jersam515
jersam515

Reputation: 657

Android- How do i get the URI of the latest file saved by the system?

In android, how could I get the URI last file saved to the system? I need this ability for an image editting application. As far as I can tell ContentObserver can only check if something has changed and neither MediaStore.Images.Media or MediaStore.Files have a method I could use. Any help would be greatly appreciated.

Upvotes: 1

Views: 3152

Answers (1)

Jean-Philippe Roy
Jean-Philippe Roy

Reputation: 4812

You could iterate trough the files in the folder(s) your application is targeting and get the newest file trough the lastModified() method then you could get it's location with the getPath() method.

public File getNewestFileInDirectory() {
   File newestFile = null;

   // start loop trough files in directory
   File file = new File(filePath);
   if (newestFile == null || file.lastModified().after(newestFile.lastModified())) {
      newestFile = file;
   }
   // end loop trough files in directory

   return newestFile;
}

Upvotes: 3

Related Questions