Search code examples
androidcursorgalleryphotos

android - cursor limit photos


How do I limit the photos till top 50? Also jho do I get most recent records here?

final String[] projection = { MediaStore.Images.Media.DATA };
       final String selection = MediaStore.Images.Media.BUCKET_ID + " = ?";
       final String[] selectionArgs = { CAMERA_IMAGE_BUCKET_ID };
       final Cursor cursor = context.getContentResolver().query(Images.Media.EXTERNAL_CONTENT_URI, 
               projection, 
               selection, 
               selectionArgs, 
               null);
       List<Image> result = new ArrayList<Image>(cursor.getCount());
       if (cursor.moveToFirst()) {
           final int dataColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
           do {
               final String data = cursor.getString(dataColumn);
               result.add(new Image(data));
           } while (cursor.moveToNext());
       }
       cursor.close();

Thank you in advance


Solution

  •   String sortOrder = String.format("%s limit 50 ", Images.ImageColumns.DATE_TAKEN + " DESC");
    
      final Cursor cursor = context.getContentResolver().query(Images.Media.EXTERNAL_CONTENT_URI, 
                    projection, 
                    selection, 
                    selectionArgs, 
                    sortOrder);
    

    sorts images by recently created date and limits them to 50 recent.