0

Is it possible to sort an Array containing Uri's by date/time?

An Uri in my array looks like:

content://media/external/images/media/65

I have tried Collections.sort() but it wasn't possible with an Uri[]

Edit: My Uri points to an image stored on the device. I want to sort the images by date and time and show it sorted in an GridView.

Lukas
  • 812
  • 1
  • 14
  • 43
  • 2
    What do you mean by `date/time`? – Krystian G Nov 03 '19 at 18:01
  • 1
    A `Uri` does not have a "date/time". It may point to *content* that might have some sort of timestamp. You are going to need to explain in greater detail what "date/time" you are referring to. – CommonsWare Nov 03 '19 at 18:03
  • 1
    Do you mean file creation date, last file access date, file modified date? – Krystian G Nov 03 '19 at 18:10
  • @KrystianG I mean file creation date. – Lukas Nov 03 '19 at 18:11
  • 1
    You can't get file creation date on Android if you're using API older that 26 (Android 8.0). You can only get last modified time. https://stackoverflow.com/questions/2389225/android-how-to-get-a-files-creation-date – Krystian G Nov 03 '19 at 18:18

1 Answers1

2

You can query the content resolver for modified time.

Uri uri = Uri.parse("content://media/external/images/media/65");

                String projection [] = { 
                          MediaStore.Images.Media.DATA
                        , MediaStore.Images.Media.DISPLAY_NAME
                        , MediaStore.Images.Media.SIZE
                        , MediaStore.Images.Media.MIME_TYPE
                        , MediaStore.Images.Media.DATE_MODIFIED
                        , DocumentsContract.Document.COLUMN_LAST_MODIFIED
                        };
                Cursor cursor = getContentResolver().query(uri, projection, null, null, null);

                if ( cursor==null)
                {   

                return; 
                }

                cursor.moveToFirst();

                String data        = cursor.getString(0);
                String displayName = cursor.getString(1);
                String size        = cursor.getString(2);
                String mimeType    = cursor.getString(3);
                String dateModified    = cursor.getString(4); // null
                String dateModified2    = cursor.getString(5);

                Toast.makeText(context,
                             "DISPLAY_NAME: " + displayName
                            + "\nDATA: " + data
                            + "\nSIZE: " + size
                                + "\nmimeType: " + mimeType
                                + "\n" +MediaStore.Images.Media.DATE_MODIFIED + ": " + dateModified
                                + "\n" +DocumentsContract.Document.COLUMN_LAST_MODIFIED + ": " + dateModified2
                            , Toast.LENGTH_LONG).show();
                cursor.close();         

Even uris from the media store deliver null for MediaStore.Images.Media.DATE_MODIFIED ("date_modified") hence DocumentsContract.Document.COLUMN_LAST_MODIFIED ("last_modified") which is good for all.

Add a try and some catch blocks.

blackapps
  • 8,011
  • 2
  • 11
  • 25