Android Studio中的文件路径
我正在尝试发送带有附件的电子邮件,并且除附件外,其他一切正常.
I'm trying to send email with attachment and everything works okay apart attaching file.
在调用方法时,它说:java.io.FileNotFoundException
When calling method, it says: java.io.FileNotFoundException
尽管我手动插入了确切的路径:文件/存储/仿真/0/Android/data/com.example.admin.mailsender/files/test.xls
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
这不是Android上的路径.充其量,这可能是有效的路径:
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")
...其中 context
是一些 Context
(您的 Activity
, Application
单例等)
...where context
is some Context
(your Activity
, the Application
singleton, etc.).