2012-04-20 115 views
1

在android中,我如何獲取保存到系統的URI最後一個文件?我需要這個功能來處理圖像編輯應用程序。據我所知,ContentObserver只能檢查是否有更改,MediaStore.Images.Media或MediaStore.Files都沒有我可以使用的方法。任何幫助將不勝感激。Android-如何獲取系統保存的最新文件的URI?

回答

3

您可以遍歷應用程序所針對的文件夾中的文件,並通過lastModified()方法獲取最新文件,然後使用getPath()方法獲取它的位置。

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

我將如何獲取目錄中的所有文件路徑? – jersam515 2012-04-20 21:55:12

+0

谷歌「循環目錄java中的所有文件」,然後只需在每個文件上調用file.getPath()... – 2012-04-20 23:50:09

+0

已接受和有投票 – jersam515 2012-04-21 00:53:34