我正在尝试发送带有附件的电子邮件,除了附加文件之外一切正常。
调用方法时,提示:java.io.FileNotFoundException
虽然我手动插入精确路径: 文件/存储/模拟/0/Android/data/com.example.admin.mailsender/files/test.xls
我实际上并没有在该函数应该查找的任何地方声明,但提供该路径还不够吗?
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 上的路径。充其量,这可能是一个有效的路径:
/storage/emulated/0/Android/data/com.example.admin.mailsender/files/test.xls
这是否是正确的路径将因设备和用户而异。对于您自己的设备上的短期测试,欢迎您使用这样的硬编码路径,但通常您应该使用方法来派生路径。在这种情况下,那就是:
new File(context.getExternalFilesDir(null), "test.xls")
...其中context
是一些Context
(您的Activity
、Application
单例等)。
关于java - Android studio中的文件路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57483156/