使用Intent.ACTION_GET_CONTENT选择多个文件

Mer*_*lez 19 android android-intent

我正在尝试用Intent选择多个文件,但似乎我缺少某些东西.
我创建了一个Intent.ACTION_GET_CONTENT Intent,将Intent.EXTRA_ALLOW_MULTIPLE作为额外的
(它似乎完全符合目的)并创建一个选择器(可选),它选择应该能够选择多个文件并返回它们的应用程序.

问题是我只能选择一个文件.

我试过多个文件浏览器.这是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);  
  }  
});
Run Code Online (Sandbox Code Playgroud)

我还将此添加到Manifest(它在添加之前具有相同的功能):

        <intent-filter>
            <action android:name="android.intent.action.GET_CONTENT" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>  
Run Code Online (Sandbox Code Playgroud)

为什么我不能选择多个文件?
(澄清:问题不在于,多个文件没有返回 - 我不能选择多个文件)

Com*_*are 9

为什么我不能选择多个文件?

据推测,"应该能够选择多个文件并返回它们的应用程序"的实现没有实现EXTRA_ALLOW_MULTIPLE支持.联系他们并请求此功能.


Kir*_*zin 7

我知道这是一个有点过时的帖子,但仍然有人遇到它:在此示例中,我们打开默认系统文件资源管理器以允许用户在其设备上选择任何类型的多个文件:

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);
Run Code Online (Sandbox Code Playgroud)


Kir*_*nna 6

我遇到了同样的问题.我的解决方案.

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();
          }
       }
    }
 } 
Run Code Online (Sandbox Code Playgroud)

科特林:

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)
                    }
                }
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)


Leo*_*eon 5

我遇到了同样的问题并ACTION_OPEN_DOCUMENT为我工作。

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());                
             }
        }
   }
}
Run Code Online (Sandbox Code Playgroud)


dip*_*ri4 5

这是我的解决方案,它对我有用,并允许我选择任何类型的文件。

// 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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)