2015-07-19 125 views
3

我知道这个问题还有很多其他帖子,但没有一个能解决我的问题。我正在尝试在Enviroment.DIRECTORY_DOWNLOADS上创建一个文件,将该文件发送给该文件,然后将其删除。但是,当我初始化文件输出流fos = new FileOutputStream(file);时出现错误。打开失败:ENOENT(没有这样的文件或目录),即使有权限

我得到的错误是:

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[]{"[email protected]"}); 
    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(); 

回答

2

使用此权限:

<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())); 
+0

我仍然得到同样的错误与该行 – Brejuro

+0

@Brejuro尝试这个编辑的代码。 –

+0

感谢您的回复,我用当前的代码编辑了我的帖子。当我点击发送它说'不幸的Gmail已经停止' – Brejuro

相关问题