Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
597 views
in Technique[技术] by (71.8m points)

android - 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)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)
                    }
                }
            }
        }
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...