我正在开发一个应用程序,希望能够从该应用程序在.txt文件中导出和导入一些数据.
该应用程序的最低API为21.
导出部分效果很好,但是导入部分遇到了麻烦.
我打开文件资源管理器:
butImportPatient.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
intent.setType("*/*");
startActivityForResult(intent, IMPORTPATIENT_ACTIVITY_REQUEST_CODE);
}
});
这看起来像在工作.
但是我的onActivityResult不起作用,我没有找到如何从Uri获取文件.
现在,这是我的代码:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == IMPORTPATIENT_ACTIVITY_REQUEST_CODE && resultCode == RESULT_OK) {
File file = new File(data.getData().getPath()) ;
String path = file.getAbsolutePath() ;
StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(path));
String line;
while ((line = br.readLine()) != null) {
text.append(line);
text.append("
");
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
AlertDialog.Builder builder = new AlertDialog.Builder(this) ;
builder.setMessage(path)
.show() ;
}
}
我在这里看到的多个帖子混合在一起,但似乎没有一个起作用.
我得到这条路:
/document/home:List.txt
它创建FileNotFoundException.如何获取文件的真实路径?