Pick Image and Pdf using single intent

here just an example:

private Intent getFileChooserIntent() {
    String[] mimeTypes = {"image/*", "application/pdf"};

    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.addCategory(Intent.CATEGORY_OPENABLE);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        intent.setType(mimeTypes.length == 1 ? mimeTypes[0] : "*/*");
        if (mimeTypes.length > 0) {
            intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
        }
    } else {
        String mimeTypesStr = "";

        for (String mimeType : mimeTypes) {
            mimeTypesStr += mimeType + "|";
        }

        intent.setType(mimeTypesStr.substring(0, mimeTypesStr.length() - 1));
    }

    return intent;
}

Upper answer did not work in my case, after a hour trial-and-error, this is my working solution:

fun getFileChooserIntentForImageAndPdf(): Intent {
        val mimeTypes = arrayOf("image/*", "application/pdf")
        val intent = Intent(Intent.ACTION_GET_CONTENT)
                .setType("image/*|application/pdf")
                .putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes)
        return intent
    }

Hope can help someone.

Tags:

Android