I want the ability to allow the user to choose an image from the gallery, but only images were taken, for example, 10 minutes ago (maybe using Time Created Metadata?).
There are examples of how to open gallery and choose an image:
Intent i = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
However, I only need images which were taken 10 minutes ago.
Does anyone have an idea?
I might be wrong but I don't think you can achieve this directly by using the Intent.ACTION_PICK
into the Gallery
. I think, there is a possible way with MediaStore.Images.ImageColumns.DATE_TAKEN
and a Cursor
as you can see in this answer. However, I did not find the right way to do it.
Then, you can do a workaround: create your own Gallery
into your app. Select all the pictures from the Camera
folder, filter them, and display only which are taken x min ago into your own Gallery
- a simple GridView
. I think this might be easy to do:
Init the var
s:
// FileArray
private File[] aFile;
// ArrayList
private ArrayList<String> aImagePath = new ArrayList<String>();
Get the DCIM Camera
folder:
File fPath = new File(Environment
.getExternalStorageDirectory().toString() + "/DCIM/Camera");
Create a FileArray
and list the files in this folder:
// 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");
}
});
For each file, get the last modified date and populate an 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
}
}
Finally, reverse the Array
to have the most recent pictures at the first place and display them:
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!
}
Actually, in the AsyncTask
, you will have to deal with the Bitmap
options to save memory... I tested this in a GridView
into a Fragment
and it worked well. You can find the references I used above:
Maybe someone has a better solution, but this above does the trick and displays only the images taken 10 min ago with the Camera
's device. I hope this will help you.