Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select multiple files with Intent.ACTION_GET_CONTENT

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)

like image 246
Mercylez Avatar asked Oct 22 '13 09:10

Mercylez


People also ask

How do I select multiple files in android programmatically?

To select multiple files press on as many files as you want to select and check marks will appear over all of the selected files. OR you press the More options menu icon in the upper right corner of the screen and press Select.

What does Android intent action view do?

A common intent action is ACTION_VIEW, which you use when you have some information that an activity can show to the user, such as a photo to view in a gallery app, or an address to view in a map app. You can specify the action for an intent in the intent constructor, or with the setAction() method.

What is intent details?

An intent allows you to start an activity in another app by describing a simple action you'd like to perform (such as "view a map" or "take a picture") in an Intent object.

How to open intent in Android?

To start an activity, use the method startActivity(intent) . This method is defined on the Context object which Activity extends. The following code demonstrates how you can start another activity via an intent.


2 Answers

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)                     }                 }             }         }     } 
like image 146
Kiran Chenna Avatar answered Sep 23 '22 01:09

Kiran Chenna


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.

like image 40
CommonsWare Avatar answered Sep 24 '22 01:09

CommonsWare