【问题标题】:File Not Found Exception when sending email with attached CSV - Android Studio发送带有附加 CSV 的电子邮件时出现文件未找到异常 - Android Studio
【发布时间】:2015-06-20 16:16:11
【问题描述】:

我有一个收集加速度计数据的应用程序,然后我希望将此数据写入 CSV 文件并通过电子邮件发送给用户。尝试发送电子邮件时会记录以下错误:

6-20 17:09:00.550    3764-4558/? E/RequestController﹕ IOException
java.io.FileNotFoundException: /data/data/com.htc.android.mail/app_mail/Download/Walk Data-20150620-170859.csv: open failed: ENOENT (No such file or directory)
        at libcore.io.IoBridge.open(IoBridge.java:406)
        at java.io.FileInputStream.<init>(FileInputStream.java:78)
        at com.htc.android.mail.server.SmtpServer.includeAttach(SmtpServer.java:948)
        at com.htc.android.mail.server.SmtpServer.issueBody(SmtpServer.java:347)
        at com.htc.android.mail.server.SmtpServer.sendMail(SmtpServer.java:153)
        at com.htc.android.mail.RequestController$SmtpThread.run(RequestController.java:2016)
 Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
        at libcore.io.Posix.open(Native Method)
        at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
        at libcore.io.IoBridge.open(IoBridge.java:390)

这是编写 CSV 并尝试发送电子邮件的 onClick 方法的代码:

public void onClick(View v) {
    switch (v.getId()) {
        case R.id.enrolBtn:
            choice = 1;
            Toast.makeText(this, "Enrolment Mode Selected", Toast.LENGTH_SHORT).show();
            break;
        case R.id.authBtn:
            choice = 2;
            Toast.makeText(this, "Authentication Service Starting", Toast.LENGTH_SHORT).show();
            break;
        case R.id.sendBtn:
            choice = 3;
            String fileName = "Walk Data.csv";
            String baseDir = getFilesDir() + "/" + fileName;
            File f = new File(baseDir);
            FileOutputStream out = null;
            try {
                out = new FileOutputStream(f);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            try {
                out.write(walkData.getBytes());
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

            android.net.Uri u1 = Uri.fromFile(f);
            Intent sendIntent = new Intent(Intent.ACTION_SEND);
            sendIntent.putExtra(Intent.EXTRA_STREAM, u1);
            sendIntent.setType("text/html");
            startActivity(sendIntent);
            break;

这是收集加速度计数据并将其存储在 walkData 字符串中的代码:

 public void onSensorChanged(SensorEvent event) {
    float x = event.values[0];
    ArrayList<Float> enrolAcc = new ArrayList<>();
    ArrayList<Float> authAcc = new ArrayList<>();
    TextView textEnrol = (TextView) findViewById(R.id.textView);
    if (choice == 1) {
        mPreviousAcc = mCurrentAcc;
        mCurrentAcc = (float) Math.sqrt((double) (x * x));
        float delta = mCurrentAcc - mPreviousAcc;
        mDiffAcc = mDiffAcc * 0.9f + delta;
        if (enrolAcc.size() < 100) {
            enrolAcc.add(x);

        } else {
            enrolAcc.remove(0);
            enrolAcc.add(x);
        }
        walkData = enrolAcc.toString();
        textEnrol.setText(walkData);
    }

提前感谢您的帮助。

【问题讨论】:

  • 您忽略了例外情况 - 您所做的只是printStack 并继续前进。在每个 catch 块内添加下一行 - Log.d("myApp", e.toString()); 然后查看 LogCat - 你会看到哪个异常首先发生并导致一系列问题
  • 您将文件放在应用程序特定的内存中,因此邮件应用程序无法访问。使文件世界可读或将其放入外部存储器中。
  • 我该如何做这个greenapps?
  • 在日志中说文件名是:Walk Data-20150620-170859.csv,但在代码中你提到的文件名是 Walk Data.csv。哪个是正确的?
  • 我假设它是代码中的那个,也就是电子邮件中附件的名称。

标签: java android android-studio filenotfoundexception


【解决方案1】:

因此,正如异常所说,它尝试在/data/data/com.htc.android.mail/app_mail/Download/ 文件夹中查找文件。而且文件不存在。如果你想创建文件,你可以试试this:

File file = new File(/*filepath*/ + /*filename*/);
file.createNewFile();
if(file.exists())
{
     // do something with it
} 

【讨论】:

    猜你喜欢
    • 2017-04-17
    • 2011-08-14
    • 1970-01-01
    • 2018-10-17
    • 2019-11-21
    • 2019-06-05
    • 2017-05-03
    • 2018-11-29
    相关资源
    最近更新 更多