首页 > 解决方案 > 无法读取多个文件

问题描述

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

标签: androidfile

解决方案


你可以试试这个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)
        }
    }

推荐阅读