Search code examples
androidonactivityresultandroid-11

Android 11 ActivityResult throwing only RESULT_CANCELED


We are selecting image/video files with this code

//Pick an image from storage
        Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
        intent.setType(type); //Can be image/* or video/*
        intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
        activityResultLauncher.launch(Intent.createChooser(intent, "Select Item"));

Then obtain the file Uri(s) on new ActivityResult API

protected final ActivityResultLauncher<Intent> activityResultLauncher = registerForActivityResult(
            new ActivityResultContracts.StartActivityForResult(),
            result -> {
                Log.wtf("WTF", result.toString());
                if (result.getResultCode() == Activity.RESULT_OK) {
                    // There are no request codes
                    Intent data = result.getData();

                    if (data == null) {
                        Toast.makeText(getContext(), R.string.unknown_error, Toast.LENGTH_SHORT).show();
                        return;
                    }

                    activityResult(true, data);
                }
                else
                    activityResult(false, null);
            });

Unfortunately after updating to Android 11 all we got is RESULT_CANCELED.

E/WTF: ActivityResult{resultCode=RESULT_CANCELED, data=null}

Solution

  • As what Ian said, you were not supposed to user Intent.createChooser with ACTION_OPEN_DOCUMENT though it works prior to Android 11.