Ask Your Question
1

What is the method to retrieve images from MediaStore during two separate time intervals in Android Studio?

asked 2023-06-13 02:21:02 +0000

lakamha gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2023-06-13 02:44:02 +0000

qstack gravatar image

To retrieve images from MediaStore during two separate time intervals in Android Studio, you can use the following steps:

  1. Create two calendar objects that represent the start and end times of each interval:
Calendar interval1Start = Calendar.getInstance();
interval1Start.set(2021, Calendar.JANUARY, 1, 0, 0, 0);

Calendar interval1End = Calendar.getInstance();
interval1End.set(2021, Calendar.JUNE, 30, 23, 59, 59);

Calendar interval2Start = Calendar.getInstance();
interval2Start.set(2021, Calendar.JULY, 1, 0, 0, 0);

Calendar interval2End = Calendar.getInstance();
interval2End.set(2021, Calendar.DECEMBER, 31, 23, 59, 59);
  1. Convert the calendar objects to milliseconds:
long interval1StartMillis = interval1Start.getTimeInMillis();
long interval1EndMillis = interval1End.getTimeInMillis();

long interval2StartMillis = interval2Start.getTimeInMillis();
long interval2EndMillis = interval2End.getTimeInMillis();
  1. Use a cursor to query the MediaStore for images within each time interval:
// For interval 1
Cursor cursor1 = getContentResolver().query(
    MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
    null,
    MediaStore.Images.Media.DATE_TAKEN + " >= ? AND " + MediaStore.Images.Media.DATE_TAKEN + " <= ?",
    new String[]{String.valueOf(interval1StartMillis), String.valueOf(interval1EndMillis)},
    MediaStore.Images.Media.DATE_TAKEN + " DESC"
);

// For interval 2
Cursor cursor2 = getContentResolver().query(
    MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
    null,
    MediaStore.Images.Media.DATE_TAKEN + " >= ? AND " + MediaStore.Images.Media.DATE_TAKEN + " <= ?",
    new String[]{String.valueOf(interval2StartMillis), String.valueOf(interval2EndMillis)},
    MediaStore.Images.Media.DATE_TAKEN + " DESC"
);
  1. Iterate through the cursor objects to retrieve the image data:
if (cursor1 != null && cursor1.moveToFirst()) {
    do {
        String imageUri = cursor1.getString(cursor1.getColumnIndex(MediaStore.Images.Media.DATA));
        // Do something with the image URI
    } while (cursor1.moveToNext());
    cursor1.close();
}

if (cursor2 != null && cursor2.moveToFirst()) {
    do {
        String imageUri = cursor2.getString(cursor2.getColumnIndex(MediaStore.Images.Media.DATA));
        // Do something with the image URI
    } while (cursor2.moveToNext());
    cursor2.close();
}

Note: This code assumes that you have the necessary READEXTERNALSTORAGE permission in your AndroidManifest.xml file.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-06-13 02:21:02 +0000

Seen: 17 times

Last updated: Jun 13 '23