【发布时间】:2018-10-10 20:08:36
【问题描述】:
我正在尝试通过打开文件资源管理器选择文本文件,然后读取所选文件。 我尝试了很多很多解决方案。最后一个就是这段代码
public void btnRead_Click(View view) {
Intent chooseFile = new Intent(Intent.ACTION_GET_CONTENT);
chooseFile.setType("text/plain");
startActivityForResult(chooseFile, 1);
}
@Override
protected void onActivityResult(int requestedCode, int resultCode, Intent data) {
if (requestedCode == 1) {
if (resultCode == RESULT_OK) {
File file = new File(data.getDataString());
StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
text.append(line);
text.append('\n');
}
br.close();
}
catch (IOException e){}
textView = (TextView) findViewById(R.id.textView);
textView.setText(text);
}
}
}
提前致谢
【问题讨论】:
-
遵循来自developer.android.com/guide/topics/providers/…的“获取输入流”说明
-
谢谢你,但不幸的是没有工作我无法替换这一行中的对象“parcelFileDescriptor”:parcelFileDescriptor.close();使用正确的对象,但我终于在这里找到了解决方案(与您的非常相似)stackoverflow.com/a/40638366/5727559 再次感谢您
标签: android android-studio android-file