react-native-cameraroll: CameraRoll.getPhotos with 'after' not working on Android sdk version 30

CameraRoll.getPhotos with ‘after’ not working on Android sdk version 30

const params = {
    first: 10,
    assetType: 'Photos'
}
if (endCursor) {
    params.after = endCursor;
}
const response = await CameraRoll.getPhotos(params);

It’s always return 10 first photos

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 19
  • Comments: 26 (1 by maintainers)

Most upvoted comments

As a quick solution try installing my fork which does resolve issue on sdk 30 pagination for my purposes. I’m not sure about sorting, I do use only newest to oldest one, which also works. Edit: I’m not going to edit the fork till the package itself will fix the issue, so don’t worry about it.

Same issue 😦

We had some issues with this as well on SDK 30. What seem to work: we moved the sorting of the query in to a bundle property QUERY_ARG_SQL_SORT_ORDER which is passed along with the content resolver query.

Example:

Cursor media = null;
String[] selectionArgsArray = selectionArgs.toArray(new String[selectionArgs.size()]);
String selectionString = selection.toString();
String sortString = Images.Media.DATE_ADDED + " DESC, " + Images.Media.DATE_MODIFIED + " DESC";

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
  Bundle selectionBundle = new Bundle();
  selectionBundle.putInt(ContentResolver.QUERY_ARG_LIMIT, mFirst + 1);
  selectionBundle.putInt(ContentResolver.QUERY_ARG_OFFSET, 0);
  selectionBundle.putString(ContentResolver.QUERY_ARG_SQL_SORT_ORDER, sortString);
  selectionBundle.putString(ContentResolver.QUERY_ARG_SQL_SELECTION, selectionString);
  selectionBundle.putStringArray(ContentResolver.QUERY_ARG_SQL_SELECTION_ARGS, selectionArgsArray);

  media = resolver.query(
    MediaStore.Files.getContentUri("external"),
    PROJECTION,
    selectionBundle,
    null
  );
} else {
  media = resolver.query(
    MediaStore.Files.getContentUri("external"),
    PROJECTION,
    selectionString,
    selectionArgsArray,
    sortString + " LIMIT " + (mFirst + 1)
  );
}

Well, You need to increment the first param by endCursor, and this getPhotos function will fetch you more photos. You will need to check for duplicates of these photos. I have made a function to solve the problem temporarily:

function getPhotos(refresh = false) {
    if (loading || (isEnd && !refresh)) return;
    setLoading(true);
    const params = { first: PAGE_SIZE };
    if (endCursor && !refresh) {
      params.after = endCursor;
      params.first = parseInt(endCursor) + PAGE_SIZE;
    }
    CameraRoll.getPhotos(params).then(
      resp => {
        setLoading(false);
        setEndCursor(resp.page_info.end_cursor);
        setIsEnd(!resp.page_info.has_next_page)
        const imgs = resp?.edges?.map(e => e.node) || []
        const currentImgs = (images || [])
        setImages(
          !refresh ? currentImgs.concat(imgs.filter(img => !currentImgs.find(cImg => cImg.image.uri === img.image.uri)))
            : (resp?.edges?.map(e => e.node) || [])
        );
      },
    );
  }

@jaspermeijaard its work correctly only if i get photos from specific album. If i try to get “All” photos, they sorting randomly(

Same issue here, sdk 30 android.