我有一个编辑文本。我想选择多个文件并读取它们并写入编辑文本(我想合并多个文件并以编辑文本显示)。目前,我只能从文件管理器中选择一个文件,并且没有问题。
我尝试了许多StackOverflow建议,但没有用。
这是用于从文件管理器中选择单个文件。
if (id == R.id.actionOpen) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath()
+ "/Speech Data/");
intent.setDataAndType(uri, "text/plain");
startActivityForResult(Intent.createChooser(intent, "Open folder"), PICK_FILE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_FILE) {
if (resultCode == RESULT_OK) {
Uri uri = data.getData();
String fileContent = readTextFile(uri);
vocalText.setText(fileContent);
}
}
}
private String readTextFile(Uri uri) {
BufferedReader reader = null;
StringBuilder builder = new StringBuilder();
try {
reader = new BufferedReader(new InputStreamReader(getContentResolver().openInputStream(uri)));
String line = "";
while ((line = reader.readLine()) != null) {
builder.append(line);
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
return builder.toString();
}
答案 0 :(得分:0)
将其放在onActivityResult
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();
}