Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intent ACTION_OPEN_DOCUMENT: browse multiple MIME type

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?

like image 665
giozh Avatar asked Oct 27 '25 08:10

giozh


1 Answers

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);
like image 145
grabarz121 Avatar answered Oct 28 '25 21:10

grabarz121



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!