Leonti
Leonti

Reputation: 10960

ACTION_OPEN_DOCUMENT with Storage Access Framework returns duplicate results

I'm trying to select images using Storage Access Framework and upload them to my server.
The problem I'm encountering is that after selecting files I get result which contains duplicates.
I have 279 photos in the folder.
ACTION_OPEN_DOCUMENT returns 279 results, but some of them are duplicates, so sometimes I get 275 unique results, sometimes 268, etc. The duplicates appear at random, so they are not specific files.

Here is the code:

int PICKER_CODE = 123;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

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

            if (intent.resolveActivity(getPackageManager()) != null) {
                startActivityForResult(Intent.createChooser(intent,
                        "Select images"), PICKER_CODE);
            }

        }
    });
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode != Activity.RESULT_OK) {
        Toast.makeText(this, "Nothing was chosen", Toast.LENGTH_LONG).show();
        return;
    }

    if (requestCode == PICKER_CODE) {

        ClipData clipData = data.getClipData();

        if (clipData != null) {

            Set<Uri> uniqueUris = new HashSet<>();

            for (int i = 0; i < clipData.getItemCount(); i++) {
                Uri photoUri = clipData.getItemAt(i).getUri();

                uniqueUris.add(photoUri);

                Log.i("IMAGE PICKER", "photo: " + photoUri);
            }

            String stats =
                    "all: " + clipData.getItemCount()
                            + ", unique: " + uniqueUris.size();

            Log.i("IMAGE PICKER", "stats: " + stats);

            Toast.makeText(this, stats, Toast.LENGTH_LONG).show();

            TextView textView = (TextView) findViewById(R.id.output_text);
            textView.setText(stats);
        }
    }

}

The full code of the activity is here: https://github.com/Leonti/android-saf-duplicates/blob/master/app/src/main/java/com/example/leonti/googledriveduplicatesdebugging/MainActivity.java

Here is the sample Android app where you can reproduce the issue: https://github.com/Leonti/android-saf-duplicates Just install it on the phone, select some images (the more the better) and it will tell you the total count and the number of unique entries.

It doesn't matter if I select photos from Google Drive or from Photos, most of the times I get duplicate results.

Can't figure out what is going wrong. I executed the same code on Android emulator (used another account and shared this Google Drive folder) and it's working as expected - all results are unique.

I'm running Android 7.0 on Nexus 5X

Thanks for any help!

Upvotes: 0

Views: 1742

Answers (1)

Leonti
Leonti

Reputation: 10960

Seems like it's a bug in Android Nougat.
I was able to reproduce it on my phone (Nexus 5X) and on emulator (Google Apis 24)
Maybe it's somehow connected to "virtual files" introduced in Nougat SAF: https://developer.android.com/about/versions/nougat/android-7.0.html#virtual_files

It works as expected on Android 6.0 emulator

Created a bug report here: https://code.google.com/p/android/issues/detail?id=224592

Upvotes: 1

Related Questions