我设法使用存储访问框架打开了一个目录。SAF 返回Uri
打开的目录。当我尝试在该目录中打开一个文件时,我FileNotFoundException
什至知道该文件确实存在(我已经检查过)。
显然我做错了什么。这是我正在尝试做的一个例子:
@TargetApi(Build.VERSION_CODES.KITKAT)
public void onActivityResult(int requestCode, int resultCode, Intent data){
if(requestCode != SELECT_FILE_REQUEST || resultCode != Activity.RESULT_OK) return;
if(data == null) return;
//get uri
Uri uri = data.getData(); // uri returnded form Intent.ACTION_OPEN_DOCUMENT_TREE
File file = new File(uri.getPath(), "text.txt");
//Read text from file
StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
text.append(line);
}
br.close();
}catch (IOException e) {
Log.e("error", e.toString()); //java.io.FileNotFoundException: /tree/primary:test/text.txt: open failed: ENOENT (No such file or directory)
}
}