File Not Found Exception when sending email with attached CSV - Android Studio

875 views Asked by At

I have an application that collects accelerometer data, I then want this data to be written to a CSV file and emailed to a user. Upon trying to send the email the following error is logged:

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)

Here is the code for the onClick method where the CSV is written and email is attempting to send:

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;

Here is the code where the accelerometer data is gathered and stored in the walkData string:

 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);
    }

Thanks in advance for any help.

1

There are 1 answers

0
Sergey Maksimenko On

So, as exception says, it tries to find file in /data/data/com.htc.android.mail/app_mail/Download/ folder. And the file is not there. If you want to create file, you can try like this:

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