Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pick Image and Pdf using single intent

Tags:

android

I know that we can use following

  1. To Pick Images :intent.setType("image/*");
  2. To Pick PDF Files :intent.setType("application/pdf");

So Is there any way by which we can pick any single entity either pdf or images through single intent?

like image 831
lokesh kalal Avatar asked Apr 19 '16 07:04

lokesh kalal


2 Answers

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;
}
like image 142
Miguel Garcia Avatar answered Oct 02 '22 18:10

Miguel Garcia


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.

like image 39
Hoang Nguyen Huu Avatar answered Oct 02 '22 17:10

Hoang Nguyen Huu