无法读取多个文件

时间:2018-07-03 07:57:08

标签: android file

 Intent intent = new Intent(Intent.ACTION_PICK);
                            intent.setType("*/*");
                            intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
                            intent.setAction(Intent.ACTION_GET_CONTENT);
                            startActivityForResult(intent, READ_REQUEST_CODE);

关于活动结果,我一直只选择一个文件。尽管我选择了多个文件。为什么呢?

List<Uri> files = Utils.getSelectedFilesFromResult(resultData);
              //  resultData.putExtra(EXTRA_ALLOW_MULTIPLE, true);
                System.out.println(resultData.getBooleanExtra(EXTRA_ALLOW_MULTIPLE,false)+"read request code"+files.size());
                for (Uri uris : files) {
                    uri = uris;//Uri.parse(uris.getPath().toString().replaceFirst("files", Environment.getExternalStorageDirectory().getPath()));
                    // file2 = Utils.getFileForUri(uri);
                    // Do something with the result...
                }

2 个答案:

答案 0 :(得分:1)

您可以在onActivityResult()中进行尝试:

    if(resultCode == Activity.RESULT_OK) {
        if(data.getClipData() != null) {
            int count = data.getClipData().getItemCount();
            for(int i = 0; i < count; i++)  
                Uri imageUri = data.getClipData().getItemAt(i).getUri();
                //do something with the image (save it to some directory or whatever you need to do with it here) 
            }
        } else if(data.getData() != null) {
            String imagePath = data.getData().getPath();
            //do something with the image (save it to some directory or whatever you need to do with it here)
        }
    }

答案 1 :(得分:0)

让我们在“活动结果”中尝试一下,

 ClipData clipdata = resultData.getClipData();
 ArrayList<Uri> mUriList = new ArrayList<>(clipdata.getItemCount());