Android Studio中的文件路径 [英] File path in Android studio

查看:130
本文介绍了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.).

这篇关于Android Studio中的文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆