【问题标题】:How to show images taken 10 minutes ago in android gallery?如何在 android 图库中显示 10 分钟前拍摄的图像?
【发布时间】:2014-04-13 20:41:35
【问题描述】:

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

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

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

但是,我只需要 10 分钟前拍摄的图像。

有人有想法吗?

【问题讨论】:

    标签: java android android-gallery


    【解决方案1】:

    我可能错了,但我认为您不能通过将Intent.ACTION_PICK 直接使用到Gallery 中来实现这一点。我认为,MediaStore.Images.ImageColumns.DATE_TAKENCursor 有一种可能的方法,如您所见in this answer。但是,我没有找到正确的方法。

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

    • 初始化vars:

      // 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&lt;String&gt;

      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 的设备拍摄的图像。我希望这会对你有所帮助。

    【讨论】:

    • 并非每部手机都将相机的照片存储在DCIM/Camera 文件夹中,有些则在DCIM/100MEDIA 中,有些在DCIM/100ANDRO ...
    • 我将从您的回答开始我的过滤照片库应用程序;)
    • 希望这会对你有所帮助,@NicolasManzini。但是,正如 Pedram 所说,要小心选择文件夹。并非所有设备都相同。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多