Android 工作室中的文件路径
File path in Android studio
我正在尝试发送带附件的电子邮件,除了附加文件外一切正常。
调用方法时,提示:java.io.FileNotFoundException
虽然我手动插入了准确的路径:
file/storage/emulated/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.io.FileNotFoundException
虽然我手动插入了准确的路径: file/storage/emulated/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
单身人士等)。