Android: gallery filtering results (date range)

I would like to allow the user to select some images from the phone’s gallery and then upload them to the application. This seems like a simple task with Intent.ACTION_PICK and startActivityForResult (intent, SELECT_PHOTO).

However, I need to filter the results in the gallery by date range. I need to set a start date and an end date, and only photos taken between the two dates should be shown (or can be selected). Does anyone know how to achieve this? It seems I can’t filter out the gallery results.

Thanks!

+4
source share
2 answers

I also came across this problem, after I tried for several hours, finally solved it :). You need to use the Media store and request its transfer from the date to today for so long that it returns an array of bitmap image. Below is the code that worked for me.

final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID, MediaStore.Images.Media.DATE_TAKEN, MediaStore.Images.Media.DATE_ADDED }; final String orderBy = MediaStore.Images.Media._ID; Cursor imagecursor = managedQuery( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, MediaStore.Images.Media.DATE_TAKEN + ">? and " + MediaStore.Images.Media.DATE_TAKEN + "<?", new String[] { "" + from, "" + to }, orderBy + " DESC"); int image_column_index = imagecursor.getColumnIndex(MediaStore.Images.Media._ID); this.count = imagecursor.getCount(); Log.w("", "count is :" + count); this.thumbnails = new Bitmap[this.count]; for (int i = 0; i < this.count; i++) { imagecursor.moveToPosition(i); int id = imagecursor.getInt(image_column_index); thumbnails[i] = MediaStore.Images.Thumbnails.getThumbnail(ctx .getApplicationContext().getContentResolver(), id, MediaStore.Images.Thumbnails.MICRO_KIND, null); } imagecursor.close(); 
+5
source

Request for images between dates:

 private Cursor cursorVar; Calendar fromDateVar = Calendar.getInstance(); fromDateVar.set(2015, 5, 30); Calendar toDateVar = Calendar.getInstance(); toDateVar.set(2016, 5, 30); cursorVar = getContentResolver().query( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, MediaStore.Images.Media.DATE_TAKEN + ">? and " + MediaStore.Images.Media.DATE_TAKEN + "<?", new String[] {fromDateVar.getTimeInMillis() + "", toDateVar.getTimeInMillis() + ""}, MediaStore.Images.Media.DATE_TAKEN + " DESC"); 
0
source

Source: https://habr.com/ru/post/1497848/


All Articles