[英]How to show last taken image from specific folder?
I want to make a feature like default camera feature did. 我想制作像默认相机功能这样的功能。 There are a left thumbnail image which show last taken image. 有一个左缩略图显示最后拍摄的图像。 When click to the photo, it show the photo and I can slide to view next photo. 点击照片后,它会显示照片,我可以滑动查看下一张照片。
My photos is put on "Abc/Pictures" folder. 我的照片放在“Abc / Pictures”文件夹中。
To get the latest modified file in folder for specific extension 获取特定扩展名的文件夹中的最新修改文件
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;
}
Addition 加成
To get all files or only png and jpeg use 获取所有文件或仅使用png和jpeg
new WildcardFileFilter("*.*");
new WildcardFileFilter(new String[]{"*.png", "*.jpg"});
Use below code find latest images from folder and I hope you are saving images with there time stamp which is standard appraoch. 使用下面的代码从文件夹中找到最新的图像,我希望你用时间戳保存图像,这是标准的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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.