5

Currently I am trying to select particular type of file from SD card or with any application but I failed, my code is as follows,

Intent intent;
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT)
            intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
        else
            intent = new Intent(Intent.ACTION_GET_CONTENT);

        intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        intent.setType("application/pdf|application/msword|application/vnd.openxmlformats-officedocument.wordprocessingml.document" +
                "|application/vnd.openxmlformats-officedocument.wordprocessingml.template" +
                "|application/vnd.ms-word.document.macroEnabled.12" +
                "|application/vnd.ms-word.template.macroEnabled.12" +
                "!application/vnd.ms-word.template.macroEnabled.12|application/rtf");

        startActivityForResult(intent, Constants.ImagePicker.REQ_PICK_RESUME);

but it just select every type of file.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Reprator
  • 2,859
  • 2
  • 32
  • 55
  • possible duplicate of https://stackoverflow.com/questions/33117592/how-to-pick-few-type-of-file-via-intent-in-android – k3b Oct 25 '17 at 13:49
  • Possible duplicate of [how to pick few type of file via intent in android?](https://stackoverflow.com/questions/33117592/how-to-pick-few-type-of-file-via-intent-in-android) – k3b Oct 25 '17 at 13:50

2 Answers2

0

try this code:

Intent intent = new Intent();
intent.setType("application/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select file"),1 );
Mehta
  • 1,228
  • 1
  • 9
  • 27
  • @i have to select particular type of file not whole, what if there is no applicaiton installed to handle the rtf type of file?? – Reprator Apr 22 '16 at 10:38
0

//click handle

 val allSupportedDocumentsTypesToExtensions = mapOf(
                "application/msword" to ".doc",
                "application/vnd.openxmlformats-officedocument.wordprocessingml.document" to ".docx",
                "application/pdf" to ".pdf"
            )
            val supportedMimeTypes = allSupportedDocumentsTypesToExtensions.keys.toTypedArray()
            val intentPDF = Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
                type = "application/pdf"
                addCategory(Intent.CATEGORY_OPENABLE)
                putExtra(Intent.EXTRA_MIME_TYPES, supportedMimeTypes)
            }
            startActivityForResult(
                Intent.createChooser(intentPDF, "Open with"),
                1001
            )

//onActivityResult

  override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    if (resultCode == Activity.RESULT_OK) {
        when (requestCode) {
1001-> {
                data?.let {
                    it.data?.also { uri ->
       val intent = Intent(Intent.ACTION_VIEW,uri)


intent.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
                         if (intent.resolveActivity(getPackageManager()) == null) {
                             Toast.makeText(this,"No app found to open Document!", Toast.LENGTH_SHORT).show()
                         } else {
                             startActivity(intent)
                         }
                    }
                }