简体   繁体   中英

File path in Android studio

I'm trying to send email with attachment and everything works okay apart attaching file.

When calling method, it says: java.io.FileNotFoundException

Although I manually insert exact path: file/storage/emulated/0/Android/data/com.example.admin.mailsender/files/test.xls

I haven't actually declared anywhere from where that function should look, but isn't providing that path enough?

    private void sendEmail() {
        //Getting content for email
        String email = "test@gmail.com";
        String subject = "Test";
        String message = "Test - body";
        String filePath = "file/storage/emulated/0/Android/data/com.example.admin.mailsender/files/test.xls";


        //Creating SendMail object
        SendMail sm = new SendMail(this, email, subject, message, filePath );

        //Executing sendmail to send email
        sm.execute();
    }
file/storage/emulated/0/Android/data/com.example.admin.mailsender/files/test.xls

This is not a path on Android. At best, this might be a valid path:

/storage/emulated/0/Android/data/com.example.admin.mailsender/files/test.xls

Whether that is the correct path will vary by device and user. For short-term testing on your own device, you're welcome to play with hard-coding paths like this, but in general you should be using methods to derive paths. In this case, that would be:

new File(context.getExternalFilesDir(null), "test.xls")

...where context is some Context (your Activity , the Application singleton, etc.).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM