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

浪子不回头ぞ 提交于 2019-12-24 10:58:50

问题


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.


回答1:


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;
}


来源:https://stackoverflow.com/questions/10238846/android-how-do-i-get-the-uri-of-the-latest-file-saved-by-the-system

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!