31

I'm trying to select multiple files with an Intent, but it seems like I'm missing on something.
I create an Intent.ACTION_GET_CONTENT Intent, put Intent.EXTRA_ALLOW_MULTIPLE as extra in
(it seems to perfectly fit the purpose) and create a chooser (optional), which chooses the application that should be able to pick multiple files and return them.

The Problem is that I can only pick a single file.

I tried multiple file explorers. It's API 18 (4.3).

ACTIVITY_CHOOSE_FILE = 1;  //global constant
Button btn = (Button) this.findViewById(R.id.btnGetFiles);
btn.setOnClickListener(new OnClickListener() {
  @Override  
  public void onClick(View v) {  
    Intent chooseFile;  
    Intent intent;  
    chooseFile = new Intent(Intent.ACTION_GET_CONTENT);  
    chooseFile.setType("file/*");  
    chooseFile.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);  
    intent = Intent.createChooser(chooseFile, "Choose a file");  
    startActivityForResult(intent, ACTIVITY_CHOOSE_FILE);  
  }  
});

I also added this to the Manifest (it had the same functionality before adding it):

        <intent-filter>
            <action android:name="android.intent.action.GET_CONTENT" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>  

Why can't I choose multiple files?
(For clarification: the problem is not, that multiple files aren't returned - I can't choose more than 1 file)

Mercylez
  • 313
  • 1
  • 3
  • 4

5 Answers5

22

I got same issue. Here my solution.

Java:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data)
    if(requestCode == PICKFILE_RESULT_CODE) {
       if(null != data) { // checking empty selection
          if(null != data.getClipData()) { // checking multiple selection or not
             for(int i = 0; i < data.getClipData().getItemCount(); i++) {
                Uri uri = data.getClipData().getItemAt(i).getUri();
             }
          } else {
             Uri uri = data.getData();
          }
       }
    }
 } 

Kotlin:

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
        super.onActivityResult(requestCode, resultCode, data)
        when (requestCode) {
            PICKFILE_RESULT_CODE -> if (resultCode === Activity.RESULT_OK) {
                if (null != data) {
                    if (null !=data.clipData) {
                        for (i in 0 until data.clipData.itemCount) {
                            val uri = data.clipData.getItemAt(i).uri
                            dumpImageMetaData(uri)
                        }
                    } else {
                        val uri = data.data
                        dumpImageMetaData(uri)
                    }
                }
            }
        }
    }
Kiran Chenna
  • 1,562
  • 1
  • 11
  • 9
  • This solution is proper because it handle the single or multiple selection files or document and returning the URIs. – dipakbari4 May 30 '19 at 15:19
14

Why can't I choose multiple files?

Presumably, the implemeters of "the application that should be able to pick multiple files and return them" have not implemented EXTRA_ALLOW_MULTIPLE support. Contact them and request this feature.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    Figured that. That's why I installed about 8 different file explorers, trying to rule that out. I guess if that's the problem, it'd be better to implement a simple file picker on my own, at least until it's a common standard. – Mercylez Oct 22 '13 at 10:22
  • 1
    @Mercylez: `EXTRA_ALLOW_MULTIPLE` was added in API Level 18, and so it will be a while before a preponderance of `ACTION_GET_CONTENT` implementers support it. In particular, it could be a bit difficult to implement, if the app does not have multiple-selection logic already for internal purposes (e.g., to support an action mode). If you absolutely have to have multiple-selection support, you have no choice but to implement it yourself. `ACTION_GET_CONTENT` will never be guaranteed to give you multiple selections. – CommonsWare Oct 22 '13 at 10:35
  • @Mercylez: FWIW, I have blogged in an attempt to get more attention on this new part of the standard: http://commonsware.com/blog/2013/10/22/action_get_content-support-extra_allow_multiple-please.html – CommonsWare Oct 22 '13 at 10:36
  • I contacted the ES App Group with the request to implement `EXTRA_ALLOW_MULTIPLE` in their file explorer. Thank you, for your help. – Mercylez Oct 22 '13 at 10:42
  • @CommonsWare hello is it possible to select specific number of image from file explorer?like 3,4,5 images..do we have that option to provide? – android dev Oct 04 '22 at 13:35
  • 1
    @apj123: On Android 11+ there may be an option for a maximum number of images via https://developer.android.com/training/data-storage/shared/photopicker. But you cannot force the user to select the maximum, and on older devices this approach is not an option. – CommonsWare Oct 04 '22 at 13:47
  • @CommonsWare sorry im pasting my query here i really need your help here--> https://stackoverflow.com/questions/74013107/how-to-fetch-data-to-show-preview-from-dynamic-link ...i will delete this comment from here after you visit my question page – android dev Oct 10 '22 at 10:24
12

I know it's a bit outdated post but still if someone encounters it: in this sample, we open default system file explorer to allow a user select multiple files of any type on his device:

Intent filesIntent;
filesIntent = new Intent(Intent.ACTION_GET_CONTENT);
filesIntent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
filesIntent.addCategory(Intent.CATEGORY_OPENABLE);
filesIntent.setType("*/*");  //use image/* for photos, etc.
startActivityForResult(filesIntent, REQUEST_CODE_FOR_ON_ACTIVITY_RESULT);
Kirill Karmazin
  • 6,256
  • 2
  • 54
  • 42
7

I got the same issue and ACTION_OPEN_DOCUMENT worked for me.

public void selectImages() {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, your_initial_uri);
    intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
    intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
    startActivityForResult(intent, 1);
}

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 1) {
        if(resultCode == RESULT_OK) {
// also check data.data because if the user select one file the file and uri will be in  data.data and data.getClipData() will be null
            for(int i = 0; i < data.getClipData().getItemCount(); i++) {
                Uri uri = data.getClipData().getItemAt(i).getUri();
                System.out.println("image" + i + "=" + uri.toString());                
             }
        }
   }
}
MohamedHarmoush
  • 1,033
  • 11
  • 17
Leon
  • 3,124
  • 31
  • 36
6

Here's the solution in my case it worked for me and allows me to select any kind of file(s).

// Start intent from your method
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
intent.setType("*/*");
startActivityForResult(intent, 1001);

// Get the result from this Overriden method
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {    
    if (resultCode == RESULT_OK) {
        switch (requestCode) {
            case 1001:

                // Checking whether data is null or not
                if (data != null) { 

                    // Checking for selection multiple files or single.
                    if (data.getClipData() != null){    

                        // Getting the length of data and logging up the logs using index
                        for (int index = 0; index < data.getClipData().getItemCount(); index++) {   

                            // Getting the URIs of the selected files and logging them into logcat at debug level
                            Uri uri = data.getClipData().getItemAt(index).getUri();
                            Log.d("filesUri [" + uri + "] : ", String.valueOf(uri) );
                        }
                    }else{ 

                        // Getting the URI of the selected file and logging into logcat at debug level
                        Uri uri = data.getData();
                        Log.d("fileUri: ", String.valueOf(uri));
                    }
                }
                break;
        }
    }
}
dipakbari4
  • 840
  • 1
  • 12
  • 16