2017-02-10 135 views
3

我想制作像默认相机功能一样的功能。有一张显示上次拍摄图像的左缩略图。点击照片时,会显示照片,我可以滑动查看下一张照片。如何显示来自特定文件夹的上次拍摄的图像?

我的照片被放在“美国广播公司/图片”文件夹。

enter image description here

+0

你想来自“Abc/Pictures”文件夹的最新照片或最新照片? – thealeksandr

+0

@thealeksandr我想要从“Abc/Pictures”文件夹中获取最新照片 – CauCuKien

回答

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

@如果文件夹包含PNG和JPG文件,我如何查询所有文件? – CauCuKien

+0

添加如何获取图片或所有文件 – thealeksandr

+0

嗨thealeksandr,感谢您的帮助。但我有一个问题与子文件夹。示例我有两个Abc文件夹的子文件夹为Abc/Pictures1和Abc/Pictures2。我如何才能从f1r Pictures1和Pictures2的文件夹中获取最新照片? – CauCuKien

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

什么是sdcardPath? – CauCuKien

+0

这会从您的代码中找到“/ Abc/Pictures”文件夹中的图像 –

+0

,它应该将sdcardPath更改为路径,对不对? – CauCuKien

相关问题