Search code examples
androidandroid-gallerygoogle-photos

Google Photos return inverse order photos when asking for EXTRA_ALLOW_MULTIPLE


I am asking for images from a gallery app with:

Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);

I am reading the results like this:

ClipData clipData = data.getClipData();
Uri[] uris = new Uri[clipData.getItemCount()];
for (int i = 0; i < uris.length; i++) {
  uris[i] = clipData.getItemAt(i).getUri();
}

I noticed that when I use Google Photos as the gallery, the results come back in a reverse order to that I have selected in Google Photos' ui.

Is that intentional, consistent or documented anywhere?


Solution

  • First, ACTION_PICK is the wrong Intent action. If you read the documentation for ACTION_PICK, that is for picking content from a specific collection; MIME type is not one of the documented input values. ACTION_GET_CONTENT is for picking content based on a MIME type.

    Second, note that EXTRA_ALLOW_MULTIPLE is for use with ACTION_GET_CONTENT and ACTION_OPEN_DOCUMENT. Hence, few ACTION_PICK implementations will honor that extra.

    Third, there is no requirement for the Uri values returned from an EXTRA_ALLOW_MULTIPLE request to be in any particular order. There are thousands of possible apps that will respond to your request, and they can send you the results in whatever order they wish. In particular, the documentation for EXTRA_ALLOW_MULTIPLE does not address order.

    If order matters in your app, build your own UI to confirm the order. Offer a convenient one-click "reverse" option, in addition to perhaps drag-and-drop to allow for arbitrary changes. After all, the user may not realize, during the content selection, that order matters, and so even the order in which the user chose the content is not the user's actual desired order.