如何显示10分钟前在android gallery中拍摄的图像?

时间:2014-04-13 20:41:35

标签: java android android-gallery

我希望能够允许用户从图库中选择图片, 但仅拍摄了图像,例如,10分钟前(可能使用时间创建的元数据?)。

有关如何打开图库和选择图像的示例:

Intent i = new Intent(Intent.ACTION_PICK, 
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);

但是,我只需要10分钟前拍摄的照片。

有没有人有想法?

1 个答案:

答案 0 :(得分:2)

我可能错了,但我不认为你可以通过Intent.ACTION_PICK使用Gallery来直接实现这一目标。我认为,MediaStore.Images.ImageColumns.DATE_TAKENCursor有一种可能的方法,你可以看到in this answer。但是,我找不到正确的方法。

然后,您可以执行一种解决方法:在您的应用中创建自己的Gallery。从Camera文件夹中选择所有图片,对其进行过滤,然后仅显示 x 分钟前的文件到您自己的Gallery - 一个简单的GridView。我认为这可能很容易做到:

  • 初始化var s:

    // FileArray
    private File[] aFile;
    // ArrayList
    private ArrayList<String> aImagePath = new ArrayList<String>(); 
    
  • 获取DCIM Camera文件夹:

    File fPath = new File(Environment
                     .getExternalStorageDirectory().toString() + "/DCIM/Camera");
    
  • 创建FileArray并列出此文件夹中的文件:

    // use a FilenameFilter to have only the pictures in JPG
    aFile = fPath.listFiles(new FilenameFilter() {
        public boolean accept(File dir, String name) {
            return name.toLowerCase().endsWith(".jpg");
        }
    });
    
  • 对于每个文件,获取上次修改日期并填充ArrayList<String>

    for(int i=0; i < aFile.length; i++) {
        long lastDate = aFile[i].lastModified();  // get last modified date
        long nowTime = nDate.getTime();  // get the current time to compare
        if(nowTime - lastDate < 600*1000) {  // if less than 10 min
            String pathFile = aFile[i].getPath(); 
            aImagePath.add(pathFile);  // populate the Array
        }
    }
    
  • 最后,反转Array以获取最新照片并显示它们:

    if(aImagePath.size() != 0) {
        Collections.reverse(aImagePath);  // reverse the Array
    
        // Call an AsyncTask to create and retrieve each Bitmap
        // then, in onPostExecute(), populate the Adapter of the GridView
    } else {
        // No pictures taken 10 min ago!
    }
    

实际上,在AsyncTask中,您必须处理Bitmap选项才能节省内存...我在GridView中对Fragment进行了测试,它运作良好。你可以找到我上面使用的参考文献:

也许某人有更好的解决方案,但上面的方法只显示10分钟前使用Camera设备拍摄的图像。我希望这会对你有所帮助。