2

I have this intent for let user choose images:

 fun launchChooseFileIntent(callerFragment: BaseFragment, REQUEST_CODE: Int) {
    val intent = Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
        addCategory(Intent.CATEGORY_OPENABLE)
        type = "image/*,"
    }

    startActivityForResult(intent, REQUEST_CODE)
}

Now i need that user can choose both images and pdf documents. I've try to change type string like

type = "image/*,application/pdf"

or like

type = "image/*|application/pdf"

but it doesn't work.

I've try also to add extra to intent like

putExtra(Intent.EXTRA_MIME_TYPES, arrayOf("image/*", "application/pdf"))

but the app crash.

How can i solve it?

giozh
  • 9,868
  • 30
  • 102
  • 183
  • "the app crash" -- use Logcat to examine the stack trace associated with your crash: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – CommonsWare May 21 '20 at 11:12

1 Answers1

6

Answer on your question is here

Here is my sample code, tested in api 28.

            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
            intent.addCategory(Intent.CATEGORY_OPENABLE);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            String[] mimeTypes = {"application/pdf", "text/csv", "text/plain"};
            intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
            intent.setType("*/*");
            intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);

            startActivityForResult(Intent.createChooser(intent, dialogTitle), requestCode);
grabarz121
  • 616
  • 5
  • 13