使用电子邮件意图Android添加附件

布莱恩·J

我已将电子邮件意图添加到Android应用程序中,并带有将附件添加为附件的代码。

但是,当我单击“电子邮件数据”按钮打开意图时,出现应用崩溃,日志猫显示以下内容,即http://hastebin.com/idejavunam.avrasm,此行输出空指针异常错误:

case R.id.emailBtn:

所以我认为这是文件uri的问题,但看不到文件存在于设备文件系统中的原因。

有谁知道我该如何调试此问题?可能我将文件的路径错误地传递给了电子邮件意图?

这是我实施该解决方案所遵循的过程。

创建CSV文件的方法中的代码:

        String baseDir = android.os.Environment.getExternalStorageDirectory().getAbsolutePath();
        String fileName = "AnalysisData.csv";
        //this filePath is used in email code and converted to Uri.
        filePath = baseDir + File.separator + fileName;
        File f = new File(filePath);

这是调用电子邮件意图的代码,其文件路径转换为用于附件的Uri:

 case R.id.emailBtn: {
             Toast.makeText(this, "email clicked", Toast.LENGTH_SHORT).show();
             Uri.fromFile(new File(filePath));
             Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
                        "mailto","[email protected]", null));
             emailIntent.putExtra(Intent.EXTRA_SUBJECT, "EXTRA_SUBJECT");
             emailIntent.putExtra(Intent.EXTRA_STREAM, filePath);
             startActivity(Intent.createChooser(emailIntent, "Send email..."));


            break;
Shubhang Malviya

我已经修改了部分检查,如果现在可以使用的话。

case R.id.emailBtn: {
             Toast.makeText(this, "email clicked", Toast.LENGTH_SHORT).show();
             Uri uri = Uri.fromFile(new File(filePath));
             Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
                        "mailto","[email protected]", null)); 
             emailIntent.setType("*/*");
             emailIntent.putExtra(Intent.EXTRA_SUBJECT, "EXTRA_SUBJECT");
             emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
             startActivity(Intent.createChooser(emailIntent, "Send email..."));


            break;

更新

同样在查看logcat之后,我发现您的文件路径为null请更正

编辑

我已经修改了您的onClick方法,只需替换一下,告诉我它是否对您有用

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    String baseDir = android.os.Environment.getExternalStorageDirectory().getAbsolutePath();
    String fileName = "AnalysisData.csv";
    filePath = baseDir + File.separator + fileName;
    File f = new File(filePath);
    switch (v.getId()) {
        case  R.id.exportBtn: {
            Toast.makeText(this, "select clicked", Toast.LENGTH_SHORT).show();
            //write sample data to csv file using open csv lib.
            date = new Date();



            CSVWriter writer = null;

            // File exist
            if(f.exists() && !f.isDirectory()){
                FileWriter mFileWriter = null;
                try {
                    mFileWriter = new FileWriter(filePath , true);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                writer = new CSVWriter(mFileWriter);
            }
            else {
                try {
                    writer = new CSVWriter(new FileWriter(filePath));
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            String data [] = new String[] {"Record Number","Ship Name","Scientist Name","Scientist Email","Sample Volume","Sample Colour","Sample Material","Latitude","Longitude","Date","\r\n"};
            writer.writeNext(data);

        /*
        //retrieve record cntr from prefs
        SharedPreferences settings = getSharedPreferences("RECORD_PREF", 0);
        recordCntr = settings.getInt("RECORD_COUNT", 0); //0 is the default value
        */

            //increment record count
            recordCntr++;

        /*
        //save record cntr from prefs
        settings = getSharedPreferences("RECORD_PREF", 0);
        SharedPreferences.Editor editor = settings.edit();
        editor.putInt("RECORD_COUNT",recordCntr);
        editor.commit();
        */
            data = new String[]{Integer.toString(recordCntr),shipName,analystName,analystEmail,sampleVolume,
                    sampleColour,sampleMaterial,latitudeValue.toString(),longitudeValue.toString(),new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date),"\r\n"};

            writer.writeNext(data);
            try {
                writer.close();
                Toast.makeText(this, "Data exported succesfully!", Toast.LENGTH_SHORT).show();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                Toast.makeText(this, "Error exporting data!", Toast.LENGTH_SHORT).show();
            }
            break;
        }

        case R.id.emailBtn: {

            Toast.makeText(this, "email clicked", Toast.LENGTH_SHORT).show();
            if (f.exists() && !f.isDirectory()) {
                Uri uri = Uri.fromFile(f);
                Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
                        "mailto","[email protected]", null));
                emailIntent.setType("*/*");
                emailIntent.putExtra(Intent.EXTRA_SUBJECT, "EXTRA_SUBJECT");
                emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
                startActivity(Intent.createChooser(emailIntent, "Send email..."));
            }


            break;
        }
    }

}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何启用在电子邮件意图Android中添加附件?

Python从函数向电子邮件添加附件

VBA无法向电子邮件添加附件

向 WordPress 插件发送的电子邮件添加附件

使用Rails使用SendGrid向电子邮件添加附件

从 excel 工作簿本身向 Outlook 电子邮件添加附件(使用 excel 事件“BeforeClose”)

如何在Excel中使用VBA向电子邮件添加附件

如何使用电子邮件属性附件计数?

如何在Google App Engine通过SendGrid(使用Java)发送的电子邮件中添加附件?

如何在curl上向电子邮件添加附件

通配符不适用于在Outlook电子邮件中添加附件的文件路径

在m子上向电子邮件添加附件的示例

在发送电子邮件之前更改文件时是否需要删除/添加附件?

向“ Google网上论坛”发送电子邮件添加附件并保存表格

如何在电子邮件中正确添加附件,特别是 IOS 中的 RTF?

使用电子邮件验证宏添加条件格式

电子邮件程序(Thunderbird),该程序使用电子邮件文本(和附件)调用ps脚本

如何使用电源自动将电子邮件附件发送到 webapi

使用电子邮件 python 库获取附件名称不起作用

需要帮助弄清楚如何在VB.NET中向电子邮件添加附件

使用电子邮件登录

使用电子邮件恢复密码

Android电子邮件意图-接收的文件附件为零字节

添加格式为“电子邮件”的附件

电子邮件意图是android吗?

采用Android意图打开电子邮件

Android - 发送短信/电子邮件 - 意图

无法使用PHPMailer在邮件中添加附件

从Android应用发送电子邮件,而无需使用电子邮件客户端