【发布时间】:2017-04-05 13:55:27
【问题描述】:
我想打开一个带有意图过滤器的 .txt 文件,但我得到了这个异常
W/System.err:java.io.FileNotFoundException:文件:/storage/emulated/0/Download/ApplicationProposal.txt:打开失败:ENOENT(没有这样的文件或目录)
在下面一行:
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);
}
}
}
在我的主要活动的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;
}
【问题讨论】:
-
不确定 Android 路径,但您说
The path is, for example: file:///storage/emulated/0/Download/filename.txt但例外有file:/(即一个斜线)作为前缀。 -
我编辑了问题,我从意图中获取路径,该意图通过意图过滤器调用我的活动
-
问题已解决:必须从路径中删除“file:///”
-
然后回答你自己的问题:P
-
the "file:///" has to be removed from the path。没有。只有"file://"。
标签: java android permissions intentfilter