存在的文本文件未找到异常
File not found exception for text file which exists
我想打开带有 intent-filter 的 .txt 文件,但出现此异常
W/System.err: java.io.FileNotFoundException: file:/storage/emulated/0/Download/ApplicationProposal.txt: open failed: ENOENT (No such file or directory)
在以下行中:
FileInputStream fis = new FileInputStream(note);
路径为,例如:
file:///storage/emulated/0/Download/filename.txt
我这样问的权限:
public void requestWritePermissions() {
if(ContextCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
if(ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE)){
Toast.makeText(getApplicationContext(), "Permission needed to export Notes to SD-Card!", Toast.LENGTH_LONG);
}
else{
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSIONS_REQUEST_STORAGE);
}
}
}
它在我的 activity
的 onCreate()
中被调用
编辑:
更多信息:
读取文件的方法是这样调用的
File toOpen = new File(intent.getData().toString());
String text = noteHandler.loadNote(toOpen);
loadNote 方法如下所示:
public String loadNote(File note){
String content = "";
try {
FileInputStream fis = new FileInputStream(note);
BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
StringBuilder data = new StringBuilder();
String line;
do{
line = reader.readLine();
if (line != null)
data.append(line).append("\n");
}
while (line != null);
content = data.toString();
reader.close();
fis.close();
} catch (Exception e){
e.printStackTrace();
Log.e("ERROR", e.toString());
}
return content;
}
您正在传递 URL 字符串并试图将其用作路径名。自然地,OS 试图将其解释为路径名......但它无法解析它。
假设你从 URL 开始,你应该做的是这样的:
URL toOpen = new URL(intent.getData().toString());
String text = noteHandler.loadNote(toOpen);
public String loadNote(URL note){
String content = "";
try {
InputStream is = note.openStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
....
请注意,字符串攻击 "file:" URL 有点冒险:
- 在某些情况下,URL 可能有不同的协议。如果您假设 URL 总是以 "file://" 开头,您可能会以不可解析的路径结束。
- 即使 "file:" URL,URL 中的某些字符也可能已被 % 编码;例如原始路径名中的 space 在 URL 中变为 %20。 OS 可能不知道如何使用 %-encoding,您将再次获得不可解析的路径。
(这些注意事项可能不适用于您的用例,但通常适用)
我想打开带有 intent-filter 的 .txt 文件,但出现此异常
W/System.err: java.io.FileNotFoundException: file:/storage/emulated/0/Download/ApplicationProposal.txt: open failed: ENOENT (No such file or directory)
在以下行中:
FileInputStream fis = new FileInputStream(note);
路径为,例如:
file:///storage/emulated/0/Download/filename.txt
我这样问的权限:
public void requestWritePermissions() {
if(ContextCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
if(ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE)){
Toast.makeText(getApplicationContext(), "Permission needed to export Notes to SD-Card!", Toast.LENGTH_LONG);
}
else{
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSIONS_REQUEST_STORAGE);
}
}
}
它在我的 activity
的onCreate()
中被调用
编辑: 更多信息:
读取文件的方法是这样调用的
File toOpen = new File(intent.getData().toString());
String text = noteHandler.loadNote(toOpen);
loadNote 方法如下所示:
public String loadNote(File note){
String content = "";
try {
FileInputStream fis = new FileInputStream(note);
BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
StringBuilder data = new StringBuilder();
String line;
do{
line = reader.readLine();
if (line != null)
data.append(line).append("\n");
}
while (line != null);
content = data.toString();
reader.close();
fis.close();
} catch (Exception e){
e.printStackTrace();
Log.e("ERROR", e.toString());
}
return content;
}
您正在传递 URL 字符串并试图将其用作路径名。自然地,OS 试图将其解释为路径名......但它无法解析它。
假设你从 URL 开始,你应该做的是这样的:
URL toOpen = new URL(intent.getData().toString());
String text = noteHandler.loadNote(toOpen);
public String loadNote(URL note){
String content = "";
try {
InputStream is = note.openStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
....
请注意,字符串攻击 "file:" URL 有点冒险:
- 在某些情况下,URL 可能有不同的协议。如果您假设 URL 总是以 "file://" 开头,您可能会以不可解析的路径结束。
- 即使 "file:" URL,URL 中的某些字符也可能已被 % 编码;例如原始路径名中的 space 在 URL 中变为 %20。 OS 可能不知道如何使用 %-encoding,您将再次获得不可解析的路径。
(这些注意事项可能不适用于您的用例,但通常适用)