答案 0 :(得分:1)
获取特定扩展名的文件夹中的最新修改文件
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.comparator.LastModifiedFileComparator;
import org.apache.commons.io.filefilter.WildcardFileFilter;
...
/* Get the newest file for a specific extension */
public File getTheNewestFile(String filePath, String ext) {
File theNewestFile = null;
File dir = new File(filePath);
FileFilter fileFilter = new WildcardFileFilter("*." + ext);
File[] files = dir.listFiles(fileFilter);
if (files.length > 0) {
/** The newest file comes first **/
Arrays.sort(files, LastModifiedFileComparator.LASTMODIFIED_REVERSE);
theNewestFile = files[0];
}
return theNewestFile;
}
添加
要获取所有文件或仅使用png和jpeg
new WildcardFileFilter("*.*");
new WildcardFileFilter(new String[]{"*.png", "*.jpg"});
答案 1 :(得分:0)
使用下面的代码从文件夹中查找最新图像,我希望您使用时间戳保存图像,这是标准的appraoch。
File path = new File(Environment.getExternalStorageDirectory()
.getPath() + "/Abc/Pictures");
int imagesCount= path .listFiles().length; // get the list of images from folder
Bitmap bmp = BitmapFactory.decodeFile(sdcardPath.listFiles()[imagesCount - 1].getAbsolutePath());
eachImageView.setImageBitmap(bmp); // set bitmap in imageview