簡體   English   中英

如何在 android 畫廊中顯示 10 分鍾前拍攝的圖像?

[英]How to show images taken 10 minutes ago in android gallery?

我希望能夠允許用戶從圖庫中選擇圖像,但只拍攝了圖像,例如,10 分鍾前(可能使用時間創建元數據?)。

有如何打開圖庫和選擇圖像的示例:

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

但是,我只需要 10 分鍾前拍攝的圖像。

有沒有人有想法?

我可能是錯的,但我認為您不能通過將Intent.ACTION_PICK用於Gallery來直接實現這一點。 我認為, MediaStore.Images.ImageColumns.DATE_TAKENCursor有一種可能的方法,如您在此答案中所見。 但是,我沒有找到正確的方法。

然后,您可以采取一種解決方法:在您的應用程序中創建您自己的Gallery Camera文件夾中選擇所有圖片,過濾它們,並僅顯示x分鍾前拍攝到您自己的Gallery - 一個簡單的GridView 我認為這可能很容易做到:

  • 初始化var

     // 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設備拍攝的圖像。 我希望這能幫到您。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM