打开失败:ENOENT(没有这样的文件或目录)即使有权限
Open failed: ENOENT (No such file or directory) even with permissions
我知道还有很多其他帖子有此问题,但 none 似乎解决了我的问题。我正在尝试在 Enviroment.DIRECTORY_DOWNLOADS
上创建一个文件,通过电子邮件发送该文件,然后将其删除。但是,当我初始化文件输出流 fos = new FileOutputStream(file);
时出现错误。
我得到的错误是:
java.io.FileNotFoundException: /CheckListReport_2015_07_19.txt: open failed: ENOENT (No such file or directory)
在我的清单中,我有 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
在应用程序标签之外
代码如下:
String report = new SimpleDateFormat("yyyy_MM_dd").format(Calendar.getInstance().getTime());
File path = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS);
File file = new File(path, "CheckListReport_" + report + ".txt");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
String lineToWrite;
for (Map.Entry entry : CheckboxHandler.data.entrySet()) {
lineToWrite = entry.getKey() + ", " + entry.getValue() + "\n";
fos.write(lineToWrite.getBytes());
}
} catch (IOException e) {
e.printStackTrace();
}
String subject = "CheckListReport_" + report + ".txt";
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("text/plain");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"checklistreportdata@gmail.com"});
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(new File(path, "CheckListReport_" + report + ".txt").toString()));
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
file.delete();
使用此权限:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
尝试使用此代码从 Download
目录读取文件:
File path = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS);
File file = new File(path, "CheckListReport_" + report + ".txt");
改变这个:
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + Environment.DIRECTORY_DOWNLOADS + "/CheckListReport_" + report + ".txt"));
至:
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(new File(path, "CheckListReport_" + report + ".txt").toString()));
我知道还有很多其他帖子有此问题,但 none 似乎解决了我的问题。我正在尝试在 Enviroment.DIRECTORY_DOWNLOADS
上创建一个文件,通过电子邮件发送该文件,然后将其删除。但是,当我初始化文件输出流 fos = new FileOutputStream(file);
时出现错误。
我得到的错误是:
java.io.FileNotFoundException: /CheckListReport_2015_07_19.txt: open failed: ENOENT (No such file or directory)
在我的清单中,我有 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
在应用程序标签之外
代码如下:
String report = new SimpleDateFormat("yyyy_MM_dd").format(Calendar.getInstance().getTime());
File path = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS);
File file = new File(path, "CheckListReport_" + report + ".txt");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
String lineToWrite;
for (Map.Entry entry : CheckboxHandler.data.entrySet()) {
lineToWrite = entry.getKey() + ", " + entry.getValue() + "\n";
fos.write(lineToWrite.getBytes());
}
} catch (IOException e) {
e.printStackTrace();
}
String subject = "CheckListReport_" + report + ".txt";
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("text/plain");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"checklistreportdata@gmail.com"});
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(new File(path, "CheckListReport_" + report + ".txt").toString()));
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
file.delete();
使用此权限:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
尝试使用此代码从 Download
目录读取文件:
File path = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS);
File file = new File(path, "CheckListReport_" + report + ".txt");
改变这个:
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + Environment.DIRECTORY_DOWNLOADS + "/CheckListReport_" + report + ".txt"));
至:
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(new File(path, "CheckListReport_" + report + ".txt").toString()));